数日間、非決定性有限オートマトン (NFA)、より具体的には文字列認識機能をシミュレートするプログラムを作成しようとしています。何度か失敗した後、ユーザーKonrad Rudolphのおかげで、次の疑似コードに基づいたソリューションを実装できました。
NFA では、現在の状態のセットがあり、各ステップで現在のすべての状態を調べ、それぞれについて、すべての有効な遷移を選択します。これらの結合されたセットは、新しい状態セットを形成します。
最後に、現在の状態と受け入れ状態の交差が空でないかどうかを確認します。
擬似コードでは、これは次のようになります。
current = { initial }
for each char in input:
next = { }
for each state in current:
for each transition in transitions[state][char]:
next.append(target_of(transition))
current = next
if len(intersection(current, accepting)) > 0:
print "String accepted"
else:
print "String rejected"
これは、1 行ずつ C++ コードに変換できます。彼はこれを簡単にすることを勧めました。std::set<int> for the current and next sets
C ++での私の実装は次のとおりです。
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <vector>
using namespace std;
int main (){
int testCases, i, j,k, cont=1,finalStates,numberInputs,stateOrigin, stateDestination;
int numberStates, numberTransitions, initialState;
int numberFinals;
char transitionCharacter ;
set<int> current;
set<int> next;
set<int>::iterator it;
set <int> final;
set<int> the_intersection; // Destination of intersect
map<pair<int, int>, char>::iterator p;
string inputString;
typedef std::pair<int, int> trigger;
std::map<trigger, char> transitions;
map<trigger, char>::iterator r;
cin>> testCases;
for (i=0;i< testCases;i++){
final.clear();
next.clear();
current.clear();
the_intersection.clear();
transitions.clear();
cin>>numberStates>>numberTransitions>>initialState>>numberFinals;
for (j=0;j<numberFinals;j++){
cin>>finalStates;
final.insert(finalStates);
}
for (j=0; j<numberTransitions;j++){
cin>> stateOrigin>>stateDestination>>transitionCharacter;
transitions.insert(make_pair(make_pair(stateOrigin, stateDestination), transitionCharacter));
}
cin>>numberInputs;
current.insert (initialState);
cout<<"Test Case #"<<cont++<<":"<<endl;
for (j=0; j<numberInputs;j++){
current.clear();
current.insert (initialState);
the_intersection.clear();
cin>> inputString;
cout<<inputString<<" ";
/// ///////////////Konrad Rudolph's solution /////////////////
for (k=0; k<inputString.size();k++){
next.clear();
for (it = current.begin(); it!=current.end(); it++){
for (r =transitions.begin(); r!=transitions.end(); r++){
if ( ((*r).first.first == *it) && ( (*r).second == inputString[k] ) ){
next.insert((*r).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.empty()){
cout<<"Rejected"<<endl;
}else {
cout<<"Acepted"<<endl;
}
/// ///////////////////////////////////////////////////////
}
cout<<endl;
}
return 0;
}
私はこの入力を持っています:
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
abc
acdddddd
予想される出力は次のとおりです。
Test Case #1:
aaabcccc Rejected
aabbbbcdc Rejected
abbcdddcc Rejected
abc Acepted
acdddddd Acepted
ただし、私のコードは出力として生成されます。
Test Case #1:
aaabcccc Rejected
aabbbbcdc Rejected
abbcdddcc Rejected
abc Acepted
acdddddd
テスト ケースの最後の文字列では、プログラムは何もせず、実行を停止しません。私の質問は、この特定の入力でプログラムがクラッシュするのはなぜですか。JFlapで同じオートマトン NFA を設計し、この最後の入力を認識します
あくどどど
.
(0, a) = 1
(1, c) = 2
(2, d) = 3
(3, d) = 4
(4, d) = 4
(4, d) = 4
(4, d) = 4
(4, d) = 5
コードの実装でどのような間違いがありますか?