Cod sursa(job #2589470)

Utilizator DawlauAndrei Blahovici Dawlau Data 26 martie 2020 13:29:51
Problema NFA Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#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';
	}
}