noip提高组2002 字串变换(luogu P1032 )

原题链接:https://www.luogu.org/problem/show?pid=1032

对于此题,我只想说,string虽然慢,但是功能太强。

还有,map大法好,不但跑的飞快,而且简化了算法,普通的bfs就解决了。

 

#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
struct node
{
    string s;
    int l;
}st;
queue<node>q;
map<string,int>vis;
string a,b,c[25][2];
int n=1;
int main()
{
    cin>>a>>b;
    while(cin>>c[n][0]>>c[n][1]) n++;
    n--;
    st.s=a;st.l=0;
    q.push(st);
    while(!q.empty())
    {
        node t=q.front();q.pop();
        if(t.s==b&&t.l<11)
        {
            printf("%d",t.l);
            return 0;
        }
        if(vis[t.s]==0)
        {
            vis[t.s]=1;
            for(int i=1;i<=n;i++)
            {
                if(t.s.find(c[i][0])>=0)
                {
                    for(int j=t.s.find(c[i][0]);j>=0&&j<=t.s.size()-c[i][0].size();j=t.s.find(c[i][0],j+1))
                    {
                        node tmp;
                        tmp.s=t.s;tmp.l=t.l+1;
                        tmp.s.replace(j,c[i][0].size(),c[i][1]);
                        q.push(tmp);
                    }
                }
            }
        }
    }
    printf("NO ANSWER!");
    return 0;
}

 

转载于:https://www.cnblogs.com/zeroform/p/7644706.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值