Pagini recente » Cod sursa (job #479583) | Cod sursa (job #2508852) | Cod sursa (job #2650113) | Cod sursa (job #399057) | Cod sursa (job #2588370)
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <climits>
#include <bitset>
using namespace std;
const int NMAX = 305;
int statesNo, edgesNo, initialState, finalStatesNo;
bool finalStates[NMAX];
bool g[NMAX][NMAX][30];
vector<int> nxt[NMAX][30];
void citire()
{
scanf("%d%d%d%d", &statesNo, &edgesNo, &finalStatesNo, &initialState);
for (int i = 0; i < finalStatesNo; i++)
{
int x;
scanf("%d", &x);
finalStates[x] = true;
}
for (int i = 0; i < edgesNo; i++)
{
int x, y;
char ch;
scanf("%d%d ", &x, &y);
ch = getc(stdin);
if (!g[x][y][ch - 'a'])
{
nxt[x][ch - 'a'].push_back(y);
}
g[x][y][ch - 'a'] = true;
}
}
char str[155];
queue<int> q;
bitset<5 + NMAX> tristutz;
bool verif()
{
fgets(str, 155, stdin);
int n = strlen(str) - 1;
q.push(initialState);
q.push(INT_MAX);
for (int i = 0; i < n; i++)
{
while (q.front() != INT_MAX)
{
int x = q.front();
for (auto const &y : nxt[x][str[i] - 'a'])
{
if (!tristutz.test(y))
{
q.push(y);
tristutz.set(y);
}
}
q.pop();
}
tristutz.reset();
q.pop();
q.push(INT_MAX);
}
bool flag = false;
while (q.front() != INT_MAX)
{
if (finalStates[q.front()])
{
flag = true;
}
q.pop();
}
q.pop();
return flag;
}
int main()
{
freopen("nfa.in", "r", stdin);
freopen("nfa.out", "w", stdout);
citire();
int testsNo;
scanf("%d\n", &testsNo);
for (int i = 0; i < testsNo; i++)
{
if (verif())
{
printf("1\n");
}
else
{
printf("0\n");
}
}
return 0;
}