P1032 字串变换
生活随笔
收集整理的這篇文章主要介紹了
P1032 字串变换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題面:https://www.luogu.org/problem/P1032
本題中需熟練掌握string類型的庫函數: string a,b; a.find(b,pos)為 從pos開始找b第一次出現的位置并返回 a.replace(pos,len,b)為 替換a中pos開始往后len的這些字符為bCode: #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; }轉載于:https://www.cnblogs.com/ukcxrtjr/p/11470589.html
總結
以上是生活随笔為你收集整理的P1032 字串变换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring框架第二天知识总结
- 下一篇: P1541 乌龟棋