Cod sursa(job #2588048)

Utilizator buhaidarius@gmail.comBuhai Darius [email protected] Data 24 martie 2020 13:15:11
Problema NFA Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#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;
    while(s--)
        g.push_back(new node(false));
    for(int i=0;i<fs;i++){
        int x;
        fin>>x;
        g[x-1]->final_state = true;
    }
    int a, b;
    char acc;
    while(m--){
        fin>>a>>b>>acc;
        g[a-1]->next.push_back({g[b-1], acc});
    }
}

int main() {
    int n;
    string ex;
    read_data();
    fin>>n;
    while(n--){
        fin>>ex;
        fout<<check_nfa(ex)<<'\n';
    }
    return 0;
}