Cod sursa(job #2588382)

Utilizator buhaidarius@gmail.comBuhai Darius [email protected] Data 24 martie 2020 18:48:37
Problema NFA Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#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;
}