Cod sursa(job #2589070)

Utilizator Johnny07Savu Ioan-Daniel Johnny07 Data 25 martie 2020 19:07:53
Problema NFA Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.29 kb
#include <bits/stdc++.h>

//#include "Automat.cpp"

using namespace std;

ifstream fin("nfa.in");

ofstream fout("nfa.out");



class Automat

{

private:

    int n, m;

    int nod_init;

    bool is_final[310];

    bool am_fost[310][160];

    int vec[310];

    vector<int> muchie[310][30];

    int n_final_nodes;

public:

    friend ifstream& operator >> (ifstream&, Automat &);

    bool checkWord(string &);

    inline void setMem(int);

};



inline void Automat :: setMem(int siz)

{

    for (int i = 0; i<= n; i++)

        for (int j = 0; j <= siz + 1; j++)

            am_fost[i][j] = 0;

}



ifstream& operator >> (ifstream& fin, Automat &obj)

{

    int aux, nods, nodf;

    char c;

    fin >> obj.n >> obj.m >> obj.n_final_nodes;

    fin >> obj.nod_init;

    for (int i = 0 ; i <= obj.n; i++)

    {

        obj.is_final[i] = false;

    }

    for (int i = 1; i<= obj.n_final_nodes; i++)

    {

        fin >> aux;

        obj.is_final[aux] = true;

    }

    for (int i = 1; i<= obj.m; i++)

    {

        fin >> nods >> nodf >> c;

        obj.muchie[nods][c - 'a'].push_back(nodf);

    }

    return fin;

}



bool Automat::checkWord(string &s)

{

    queue <int> q_n;

    queue <int> q_l;

    int nod, l;

    int siz = s.size();

    q_n.push(nod_init);

    q_l.push(0);

    while (!q_n.empty())

    {

        nod = q_n.front();

        l = q_l.front();



        q_n.pop();

        q_l.pop();

        if (l == siz)

        {

            if (is_final[nod] == 1) return 1;

        }

        else

        {

            for (auto next : muchie[nod][s[l] - 'a'])

                if (!am_fost[next][l + 1])

                    {
                        am_fost[next][l + 1] = true;

                        q_n.push(next);

                        q_l.push(l + 1);

                    }

        }

    }

    return 0;

}





int main()

{

    Automat aut;

    fin >> aut;

    string s;

    int n;

    fin >> n;

    for (int i = 1 ; i <= n; i++)

    {

        fin >> s;

        aut.setMem(s.size());

        fout << aut.checkWord(s) <<"\n";

    }



    return 0;



}