Pagini recente » Cod sursa (job #1703096) | Cod sursa (job #2539484) | Cod sursa (job #29057) | Cod sursa (job #1604904) | Cod sursa (job #2589470)
#include <fstream>
#include <list>
#include <bitset>
#include <unordered_map>
#include <string>
using namespace std;
const int maxV = 300;
const int maxLen = 150;
ifstream fin("nfa.in");
ofstream fout("nfa.out");
list < pair <int, int> > adjList[1 + maxV];
int V, E, szF, S;
unordered_map <int, bool> F;
bitset <maxLen> seen[1 + maxV];
void readData() {
fin >> V >> E >> szF;
fin >> S;
for (; szF; --szF) {
int state;
fin >> state;
F[state] = true;
}
for (; E; --E) {
int from, to;
char ch;
fin >> from >> to >> ch;
adjList[from].push_back({ to, ch });
}
}
bool found;
void DFS(int node, const string &word, const int &idx) {
if (found) return;
if (idx == (int)word.size()) {
found = (F.count(node) != 0);
return;
}
seen[node][idx] = true;
for (const pair <int, char> &nextNode : adjList[node])
if (!seen[nextNode.first][idx + 1] and word[idx] == nextNode.second)
DFS(nextNode.first, word, idx + 1);
}
int main() {
readData();
int Q;
fin >> Q;
for (; Q; --Q) {
string word;
fin >> word;
for (int node = 1; node <= V; ++node)
seen[node].reset();
found = false;
DFS(S, word, 0);
fout << found << '\n';
}
}