Pagini recente » Cod sursa (job #1837718) | Cod sursa (job #1510019) | Cod sursa (job #415814) | Cod sursa (job #2886159) | Cod sursa (job #3217235)
#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 NFA(std::string& str) {
std::stack<std::pair<int, int>> possible_states;
possible_states.push({ S,0 });
while (!possible_states.empty()) {
std::pair<int, int> state_index = possible_states.top();
possible_states.pop();
int state = state_index.first;
int index = state_index.second;
if (index < str.length()) {
if (states[state][str[index]].empty()) {
continue;
}
for (auto& possible_state : states[state][str[index]]) {
possible_states.push({ possible_state,index + 1 });
}
}
else if (index == str.length()) {
if (final_states.find(state) != final_states.end()) { return true; }
}
}
return false;
}
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;
if (NFA(str)) { out << 1 << std::endl; }
else { out << 0 << std::endl; }
}
return 0;
}