Designing non-deterministic finite state machines in C ++ (incorrect output)

I am performing a task to simulate a non-deterministic finite state machine, as I explain in this post. I have this input read from a file tarea4.in:

1
6 8 0 2
2
5
0 0 a
0 1 a
1 1 b
1 2 c
1 3 c
3 4 d
4 4 d
4 5 d
5
aaabcccc
aabbbbcdc
abbcdddcc
acdddddd
abc

The first line of input is an integer T representing the number of cases for evaluating the program. Each test case starts with 4 integers, the first is the number of states for the automaton, then the number of transitions of the automaton, the third number is the initial state, and then the number of final states. then the final states come (in the example, the final states are 2 and 5). Then come F lines, each of which has an integer E representing E, is the final state.

N (N - ), 2 I, J C, , , .. J C. S, , S .

:

Test Case #2:
aaabcccc Rejected
aabbbbcdc Rejected
abbcdddcc Rejected
acdddddd Accepted
abc Accepted

, :

Test Case #1:
aaabcccc Rejected
aabbbbcdc Rejected
abbcdddcc Rejected
acdddddd Rejected
abc Rejected

:

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <vector>    
using namespace std;

typedef map<pair<int, int>, char> transitions;
    transitions trans;

    int  numberFinals;
    vector<int> currentStates;    

int main (){ 

    freopen ("tarea4.in", "r", stdin);
    //freopen ("tarea4.out", "w", stdout);        
    int testCases, i, j,k, cont=1,finalStates,numberInputs,stateOrigin, stateDestination;
    int numberStates, numberTransitions, initialState;
    char transitionCharacter ;

    set<int> current;
    set<int> next;
    set<int>::iterator it;
    set <int> final;
    std::set<int> the_intersection;  // Destination of intersect
    map<pair<int, int>, char>::iterator p;
    string inputString;

    cin>> testCases;
    for (i=0;i< testCases;i++){
        cin>>numberStates>>numberTransitions>>initialState>>numberFinals;
        current.insert (initialState);

        for (j=0;j<numberFinals;j++){
            cin>>finalStates;
            final.insert(finalStates);
        }

        for (j=0; j<numberTransitions;j++){
            cin>> stateOrigin>>stateDestination>>transitionCharacter;
            trans.insert(transitions::value_type(std::make_pair(stateOrigin, stateDestination), transitionCharacter ));
       }

        cin>>numberInputs;

        cout<<"Test Case #"<<cont++<<":"<<endl;    

        for (j=0; j<numberInputs;j++){
             //////////////////the code of the answer /////////////////
            current.insert (initialState);
            cin>> inputString;
            cout<<inputString<<" ";


     for (k=0; k<str.size();k++){
         next.clear();
         for ( it=current.begin() ; it != current.end(); it++ ){
              for (q= trans.begin(); q!= trans.end();q++){
                  if((*it == q->first.first)&&(str[k]==q->second)){
                     next.insert(q->first.second);
                   }
              current=next;
              }
         }
     }

            std::set_intersection(current.begin(), current.end(), final.begin(), final.end(), std::inserter(the_intersection, the_intersection.end()));

            if (the_intersection.size()>0){
                cout<< "Accepted"<<endl;
            }
            else{
                cout<< "Rejected"<<endl;
            }

        }

        printf ("\n");
    }

return 0;
}

: ? , , , ?. evaluate_string , - , ?

, , , .

+5
3

NFA , DFA.

DFA , . , .

, NFA , , . .

, .

:

  • current= { }
  • char :
    • next= {}
    • current:
      • [ state] [ char] ∪ [ state] [ ε]:
        • < > . < > Append (target_of ( ))
    • current= next
  • , len ( ( current, )) > 0:
    • print "String accepted"
  • :
    • print "String rejected"

++. , std::set<int> current next std::multimap<char, int> . , .

+9

, NFA DFA. , DFA .

+1

, , . , DFA, NFA .

( , ):

1) Implementation of an NFA evaluator: this includes tracking a set of current states and evaluating each input symbol for each state. After the line has been read, if any of the last states is in the set, it is completed.

2) Convert NFA to DFA, that is, IMHO is a more complex approach, since this is mainly related to building the same set logic and calculating transitions for new states.

+1
source

All Articles