首页 > 其他 > 详细

P1032 字串变换

时间:2019-09-06 00:15:53      阅读:110      评论:0      收藏:0      [点我收藏+]

题面: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;
}

P1032 字串变换

原文:https://www.cnblogs.com/ukcxrtjr/p/11470589.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!