P1032 字串变换(BFS)

https://www.luogu.org/problem/P1032

emmm……感觉这个题不是在练习搜索,是练习找bug

做这个题,首先要想好怎么存储。
(1)DFS肯定要用到队列
(2)匹配关系用map来存储吗?(答案是不能,因为可能会有多个匹配问题,而map是一对一的关系,就比如说"x"->"xy","x"->"z"这两个存储关系,用map来存的话只会存储一个键为x的),我采用了结构体来存储:

struct MAP
{
    string x;
    string y;
}m[55];

(3)标记vis数组怎么来存储,我是用map来存储的
map<string,int> vis;

下面来说一下这个题的坑:
(1)题目没说都存的是字母,所以在更改字符串时要用一个不常见的符号。
(2)刚刚也讲到了,多重匹配的问题,不能用map来存储匹配规则。
(3)一个串中可以会满足同一个匹配规则多次,所以需要用到循环。

            while(1)
            {
                int pos = tmp.find(itf);
                if(pos==-1)
                    break;
                tmp[pos]='?';
                //把刚刚找到的位置赋上不同的,
                //下一次就可以在用到find函数了,
                //这样保证find函数找到的位置跟之前的不同
                //但是需要注意的是不能将改变过的放入队列里面
                if(pos!=-1)
                {
                    //简单的截取字符串,拼接字符串,最好写完这一部分测试一下
                    string t = "";
                    t = now.s.substr(0, pos);
                    t += its;
                    t+=now.s.substr(pos + itf.size(), now.s.size() - pos - itf.size());
                    start.s = t;
                    start.time = now.time + 1;
                    if (vis[t]==0)
                    {
                        q.push(start);
                        vis[t]=1;
                    }
                }
            }


其实现在就没啥应该错的了,但是我在第一组样例上错了几次,其实第一组样例就是上面说的坑(3),如果你错了第一组样例,那就试试这一组样例

zxct dfrg
zx cd
zx df
fct frg


答案应该是2。
我错的原因就是上面代码中tmp和now.s弄混了,是这组样例调试才发现。

下面是AC代码:(C++)
 

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <queue>
#include<map>
#include<set>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int MAXN=30000010;
int n = 0;
struct node
{
    string s;
    int time;
} start, now;
queue<node> q;
string A, B;
struct MAP
{
    string x;
    string y;
}m[55];
map<string,int> vis;
int bfs()
{
    start.s = A;
    start.time = 0;
    vis[A]=1;
    q.push(start);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        if(now.time>10)
            return -1;
        if(now.s==B)
            return now.time;
        for(int i=0;i<n;i++)
        {
            string itf = m[i].x;
            string its = m[i].y;
            string tmp=now.s;
            while(1)
            {
                int pos = tmp.find(itf);
                if(pos==-1)
                    break;
                tmp[pos]='?';
                if(pos!=-1)
                {
                    string t = "";
                    t = now.s.substr(0, pos);
                    t += its;
                    t+=now.s.substr(pos + itf.size(), now.s.size() - pos - itf.size());
                    start.s = t;
                    start.time = now.time + 1;
                    if (vis[t]==0)
                    {
                        q.push(start);
                        vis[t]=1;
                    }
                }

            }
        }
    }
    return -1;
}
int main()
{
    cin >> A >> B;
    string a, b;
    while (cin >> a >> b)
    {
        m[n].x=a;
        m[n].y=b;
        n++;

    }
    int ans=bfs();
    if(ans==-1)
        cout << "NO ANSWER!" << endl;
    else
        cout << ans << endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值