标签:string类 https cout 开始 math mat while answer namespace
题面:https://www.luogu.org/problem/P1032
本题中需熟练掌握string类型的库函数:
string a,b;
a.find(b,pos)为
从pos开始找b第一次出现的位置并返回
a.replace(pos,len,b)为
替换a中pos开始往后len的这些字符为b
Code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctime>
#include<queue>
using namespace std;
const int N=10;
string a,b,ar[N],br[N];
int len=1,pos,tot;
struct Node{
string now;
int step;
};
deque<Node> Q;
int main(){
cin>>a>>b;
while(cin>>ar[len]>>br[len]){
len++;
}
len--;
Q.push_front((Node){a,0});
while(!Q.empty()){
if(Q.front().step>10){
cout<<"NO ANSWER!"<<endl;
return 0;
}
for(int j=1;j<=len;j++){
pos=Q.front().now.find(ar[j],0);
while(pos!=-1){
string c=Q.front().now;
c.replace(pos,ar[j].size(),br[j]);
Q.push_back((Node){c,Q.front().step+1});
if(Q.back().now==b){
printf("%d\n",Q.back().step);
return 0;
}
pos=Q.front().now.find(ar[j],pos+1);
}
}
Q.pop_front();
}
return 0;
}
标签:string类 https cout 开始 math mat while answer namespace
原文地址:https://www.cnblogs.com/ukcxrtjr/p/11470589.html