Cod sursa(job #2588050)

Utilizator buhaidarius@gmail.comBuhai Darius [email protected] Data 24 martie 2020 13:18:25
Problema NFA Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_set>

using namespace std;

struct node{
    vector<pair<node *, char>> next;
    bool cinal_state;
    node(int is_cinal_state){
        this->cinal_state = is_cinal_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->cinal_state)
                        end_state = true;
                }
            }
        states = new_states;
    }
    return end_state;
}

void read_data(){
    int a, b, x;
    char acc;
    g.clear();
    cin>>s>>m>>fs>>ss;
    while(s--) g.push_back(new node(false));
    while(fs--){
        cin>>x;
        g[x-1]->cinal_state = true;
    }
    while(m--){
        cin>>a>>b>>acc;
        g[a-1]->next.push_back({g[b-1], acc});
    }
}

int main() {
    
    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';
    }
    return 0;
}