第二个测试样例应该输出8,但是答案是9,是不是有问题啊 附上第二个测试点数据:
((T+F)+(F+F))+(F+T) F T+T F T+F T F+T T F+F F (T) T (F) F
题目给的输出是9,我觉得8次也行
下面是我跑出来的
F
T+T
(T)+T
(T+F)+T
(T+(F))+T
((T)+(F))+(T)
((T)+(F+F))+(T)
((T)+(F+F))+(F+T)
((T+F)+(F+F))+(F+T)
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 1e6;
string a, b;
struct node{
string s1, s2;
}s[10];
int cnt;
struct node1{
string str;
int step, f;
}deq[N];
int front, tail, ans, tag;
map<string, int> Maps;
int main()
{
cin>>a>>b;
while (cin>>s[cnt].s1>>s[cnt].s2) cnt++;
deq[tail].str = a; deq[tail].step = 0;deq[tail].f = -1;
tail++; Maps[a] = 0;
while (front < tail){
node1 tn = deq[front++];
if (tn.step >= 10) continue;
for (int i = 0; i < cnt; i++){
if (tn.str.find(s[i].s1) == string::npos) continue;
string ts = tn.str;
int index = 0;
while (true){
index = ts.find(s[i].s1, index);
if (index == string::npos) break;
ts.replace(index, s[i].s1.length(), s[i].s2);
index += s[i].s1.length();
// cout<<"ts == "<<ts<<endl;
}
if ((Maps.count(ts) && Maps[ts] > tn.step + 1) || !Maps.count(ts)){
Maps[ts] = deq[tail].step = tn.step + 1;
deq[tail].str = ts; deq[tail].f = front - 1;
tail++;
}
if (ts == b){
tag = 1;
ans = deq[tail - 1].step;
break;
}
}
if (tag) break;
}
if (tag) cout<<ans<<endl;
else cout<<"NO ANSWER!"<<endl;
int move = tail - 1;
while (move != -1){
cout<<deq[move].str<<endl;
move = deq[move].f;
}
return 0;
}