


#include<cstdio>
#include<string>
#include<map>
#include<iostream>
#include<queue>
using namespace std;
int main(){
string A,B;
string x,y;
cin>>A>>B;
map<string,string>s;
while(cin>>x>>y){
s[x]=y;
}
queue<string>A0;
queue<int>p;
A0.push(A);
p.push(0);
while(!A0.empty()){
if(A0.front()==B){//當找到時輸出
cout<<p.front();
return 0;
}
if(p.front()>=10){
A0.pop();
p.pop();
}
string t;
int kai,len=0;
t=A0.front();
map<string,string>::iterator it;
for(it=s.begin();it!=s.end();it++){
if(kai=t.find(it->first,0)){
len=it->first.size();
t.replace(kai,len,it->second);
cout<<kai<<len<<it->second<<endl;
A0.push(t.substr(0,kai)+it->second+t.substr(kai+len));
p.push(p.front()+1);
}
}
A0.pop();
p.pop();
}
printf("NO ANSWER!\n");
return 0;
}
uj5u.com熱心網友回復:
#include<cstdio>#include<string>
#include<map>
#include<iostream>
#include<queue>
using namespace std;
int main() {
string A, B;
string x, y;
cin >> A >> B;
map<string, string>s;
while (cin >> x >> y) {
s[x] = y;
}
queue<string> A0;
queue<int> p;
A0.push(A);
p.push(0);
while (!A0.empty()) {
if (A0.back() == B) {//當找到時輸出
cout << p.back();
return 0;
}
if (p.back() >= 10) {
A0.pop();
p.pop();
}
string t;
int kai, len = 0;
t = A0.back();
map<string, string>::iterator it;
for (it = s.begin(); it != s.end(); it++) {
if ((kai = t.find(it->first)) != -1) {
len = it->first.size();
t.replace(kai, len, it->second);
cout << kai << len << it->second << endl;
A0.push(t);
p.push(p.back() + 1);
}
}
A0.pop();
p.pop();
}
printf("NO ANSWER!\n");
return 0;
}
uj5u.com熱心網友回復:
感謝大佬,我懂了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/117274.html
標籤:C++ 語言
上一篇:大哥們這個怎么搞
下一篇:求答
