Cod sursa(job #2589288)

Utilizator DawlauAndrei Blahovici Dawlau Data 26 martie 2020 00:43:49
Problema NFA Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#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';
	}
}