Cod sursa(job #2588383)

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

#define MAXN 1201

using namespace std;

bool viz[MAXN][MAXN];

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';
        for(int i=0;i<MAXN;i++)
            for(int j=0;j<MAXN;j++)
                viz[i][j] = 0;
    }
}

int main() {

    execute_test();

    return 0;
}