Pagini recente » Cod sursa (job #949257) | Cod sursa (job #612528) | Cod sursa (job #780720) | Cod sursa (job #38515) | Cod sursa (job #2588117)
#include <cstdio>
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <queue>
#include <climits>
using namespace std;
const int NMAX = 305;
struct Automat
{
int statesNo, edgesNo, initialState, finalStatesNo;
bool finalStates[NMAX];
bool g[NMAX][NMAX][30];
vector<int> nxt[NMAX][30];
} myAutomata;
vector<string> tests;
void citire()
{
scanf("%d%d%d%d", &myAutomata.statesNo, &myAutomata.edgesNo, &myAutomata.finalStatesNo, &myAutomata.initialState);
for (int i = 0; i < myAutomata.finalStatesNo; i++)
{
int x;
scanf("%d", &x);
myAutomata.finalStates[x] = true;
}
for (int i = 0; i < myAutomata.edgesNo; i++)
{
int x, y;
char ch;
scanf("%d%d ", &x, &y);
ch = getc(stdin);
if (!myAutomata.g[x][y][ch - 'a'])
{
myAutomata.nxt[x][ch - 'a'].push_back(y);
}
myAutomata.g[x][y][ch - 'a'] = true;
}
int testsNo;
scanf("%d", &testsNo);
for (int i = 0; i < testsNo; i++)
{
string str;
cin >> str;
tests.push_back(str);
}
}
bool verif(string str)
{
queue<int> q;
q.push(myAutomata.initialState);
q.push(INT_MAX);
for (int i = 0; i < str.size(); i++)
{
while (q.front() != INT_MAX)
{
int x = q.front();
for (auto y : myAutomata.nxt[x][str[i] - 'a'])
{
q.push(y);
}
q.pop();
}
q.pop();
q.push(INT_MAX);
}
while (q.front() != INT_MAX)
{
if (myAutomata.finalStates[q.front()])
{
return true;
}
q.pop();
}
return false;
}
int main()
{
freopen("nfa.in", "r", stdin);
freopen("nfa.out", "w", stdout);
citire();
for (auto test : tests)
{
if (verif(test))
{
printf("1\n");
}
else
{
printf("0\n");
}
}
return 0;
}