Pagini recente » Cod sursa (job #3142431) | Cod sursa (job #1545907) | Cod sursa (job #1231280) | Cod sursa (job #2791918) | Cod sursa (job #2589288)
#include <fstream>
#include <list>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
ifstream fin("nfa.in");
ofstream fout("nfa.out");
const int VMAX = 300; // nr noduri
list < pair <int, char> > adjList[1 + VMAX];
unordered_map <int, bool> finalStates; // retin starile finale intr-un hashmap ca sa le accesez mai usor
int transitions;
int initState;
int finalStateCnt;
int stateCnt;
void readData() {
fin >> stateCnt >> transitions >> finalStateCnt;
fin >> initState;
for (; finalStateCnt; --finalStateCnt) {
int finalState;
fin >> finalState;
finalStates[finalState] = true;
}
for (; transitions; --transitions) {
int from, to;
char ch;
fin >> from >> to >> ch;
adjList[from].push_back({ to, ch });
}
}
bool check(int node, const string &word, int idx) {
if (idx == (int)word.size())
return (finalStates.find(node) != finalStates.end());
bool ok = false;
for (const pair <int, char> &nextNode : adjList[node])
if (nextNode.second == word[idx])
ok |= check(nextNode.first, word, idx + 1);
return ok;
}
int main() {
readData();
int wordsCnt;
fin >> wordsCnt;
for (; wordsCnt; --wordsCnt) {
string word;
fin >> word;
fout << check(initState, word, 0) << '\n';
}
}