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的这些字符为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;
}

转载于:https://www.cnblogs.com/ukcxrtjr/p/11470589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值