Pagini recente » Cod sursa (job #3282021) | Cod sursa (job #2277189) | Cod sursa (job #1058216) | Cod sursa (job #1684643) | Cod sursa (job #2588043)
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_set>
using namespace std;
ifstream fin("nfa.in");
ofstream fout("nfa.out");
struct node{
vector<pair<node *, char>> next;
bool final_state;
node(int is_final_state){
this->final_state = is_final_state;
}
};
vector<node *> g;
int s, m, fs, ss;
bool check_nfa(string input){
unordered_set<node*> states, new_states;
states.insert(g[ss-1]);
bool end_state;
for(char l : input){
end_state = false;
new_states.clear();
for(auto state : states)
for(auto next_state: state->next){
if(next_state.second==l){
new_states.insert(next_state.first);
if(next_state.first->final_state)
end_state = true;
}
}
states = new_states;
}
return end_state;
}
void read_data(){
g.clear();
fin>>s>>m>>fs>>ss;
for(int i=0;i<s;i++)
g.push_back(new node(false));
for(int i=0;i<fs;i++){
int x;
fin>>x;
g[x-1]->final_state = true;
}
while(m){
int a, b;
char acc;
fin>>a>>b>>acc;
g[a-1]->next.push_back({g[b-1], acc});
m--;
}
}
int main() {
int n;
string ex;
read_data();
fin>>n;
while(n){
fin>>ex;
fout<<check_nfa(ex)<<'\n';
n--;
}
return 0;
}