Pagini recente » Cod sursa (job #1195527) | Cod sursa (job #2666026) | Cod sursa (job #3004174) | Cod sursa (job #511958) | Cod sursa (job #2588382)
#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_set>
using namespace std;
bool viz[1201][1201];
struct node{
vector<pair<node *, char>> next;
int id;
bool final_state;
node(int id, int is_final_state){
this->final_state = is_final_state;
this->id = id;
}
};
vector<node *> g;
int s, m, fs, ss;
bool check_nfa(string input){
int k = 0;
vector<node*> states, new_states;
states.push_back(g[ss-1]);
bool end_state;
for(char l : input){
k++;
end_state = false;
new_states.clear();
for(auto state : states)
for(auto next_state: state->next){
if(next_state.second==l && !viz[next_state.first->id][k]){
viz[next_state.first->id][k] = 1;
new_states.push_back(next_state.first);
if(next_state.first->final_state)
end_state = true;
}
}
states = new_states;
}
return end_state;
}
void read_data(){
int a, b, x;
char acc;
cin>>s>>m>>fs>>ss;
for(int i=0;i<s;i++)
g.push_back(new node(i,false));
while(fs--){
cin>>x;
g[x-1]->final_state = true;
}
while(m--){
cin>>a>>b>>acc;
g[a-1]->next.push_back({g[b-1], acc});
}
}
void execute_test(){
freopen("nfa.in", "r", stdin);
freopen("nfa.out", "w", stdout);
int n;
string ex;
read_data();
cin>>n;
while(n--){
cin>>ex;
cout<<check_nfa(ex)<<'\n';
memset(viz,0, sizeof(viz));
}
}
int main() {
execute_test();
return 0;
}