Cod sursa(job #3217243)

Utilizator raizoSoare Antonio raizo Data 21 martie 2024 22:09:39
Problema NFA Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <stack>

std::ifstream in("nfa.in");
std::ofstream out("nfa.out");

int x, y, n, m, S, nrF, nrCuv;
char l;
std::set<int> final_states;
std::map<int, std::map<char, std::vector<int>>> states;
std::string str;
bool succesful;

void NFA(std::string str, int state, int index) {
	if (!succesful) {
		if (index < str.length()) {
			for (int possible_state : states[state][str[index]]) {
				NFA(str,possible_state, index + 1);
			}
		}
		else if (index == str.length()) {
			if (final_states.find(state) != final_states.end()) { succesful = true; }
		}
	}
}

int main() {

	in >> n >> m >> nrF;
	in >> S;
	for (int i = 0; i < nrF; i++) {
		in >> x;
		final_states.insert(x);
	}
	for (int i = 0; i < m; i++) {
		in >> x >> y;
		in >> l;
		in.get();
		states[x][l].push_back(y);
	}
	in >> nrCuv;
	for (int i = 0; i < nrCuv; i++) {
		in >> str;
		succesful = false;
		NFA(str,S, 0);
		if (succesful) { out << 1 << std::endl; }
		else { out << 0 << std::endl; }
	}
	return 0;
}