题目链接:https://www.luogu.org/problemnew/show/P1032

题意:

给定一个原字符串和目标字符串,以及几个字符串变换的规则。

问能否根据这几个规则在十步之内把原字符串变为目标字符串。

思路:

bfs,队列维护字符串和所经过的步骤这样一个二元组而不是简单的字符串。

每一次取出一个字符串,用所给的规则进行变换得到新的字符串。

由于字符串中可能有多次匹配,所以用vector存每一次匹配的开始位置,这些都是独立的一次变换都要单独存入vector中。【这一点最开始没有考虑到,图方便用了.find()】

 #include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue> using namespace std; string A, B;
struct rule{
string a, b;
}rules[];
struct node{
string s;
int steps;
node(){}
node(string _s, int _steps){
s = _s;
steps = _steps;
}
};
set<string>sset; int main()
{
cin>>A>>B;
int n = ;
while(cin>>rules[n].a>>rules[n].b)n++; queue<node>que;
que.push(node(A, ));
int steps = ;
bool flag = false;
while(!que.empty()){
node now = que.front();que.pop();
//cout<<now.s<<" "<<now.steps<<endl;
if(now.s == B){
flag = true;
steps = now.steps;
break;
}
if(now.steps == )continue;
for(int i = ; i < n; i++){
//cout<<i<<endl;
vector<int>pos;
//cout<<rules[i].a.length()<<endl;
//cout<<now.s.length()<<endl;
//cout<<now.s.length() - rules[i].a.length()<<endl;
for(int j = ; j < now.s.length(); j++){
bool f = true;
for(int k = ; k < rules[i].a.length(); k++){
if(j + k >= now.s.length()){
f = false;
break;
}
if(now.s[j + k] != rules[i].a[k]){
f = false;
break;
}
}
if(f)pos.push_back(j);
} //cout<<pos.size()<<endl;
for(int x = ; x < pos.size(); x++){
int p = pos[x];
char t[];
int j;
for(j = ; j < p; j++){
t[j] = now.s[j];
//cout<<t[j];
}
for(int k = ; k < rules[i].b.length(); k++, j++)
{
t[j] = rules[i].b[k];
}
for(int k = p + rules[i].a.length(); k < now.s.length(); k++, j++){
t[j] = now.s[k];
}
t[j] = '\0'; if(sset.find(t) == sset.end()){
que.push(node(t, now.steps + ));
sset.insert(t);
}
}
}
}
if(flag){
printf("%d\n", steps);
}
else{
printf("NO ANSWER!\n");
}
return ;
}

---恢复内容结束---

洛谷P1032 字串变换【bfs】的更多相关文章

  1. [洛谷P1032] 字串变换

    洛谷题目链接:字串变换 题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B ...

  2. 洛谷 P1032 字串变换题解

    题目链接:https://www.luogu.org/problem/P1032 题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1​ ->B_1B1​ A ...

  3. 洛谷 P1032 字串变换

    题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...

  4. 洛谷 P1032 字串变换 (BFS)

    题目传送门 我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出 STL大法好 这题最麻烦的其实是处理字符串,真正的搜索部分我个人认为也就只有橙题或黄题的难度.而处理字符串,正如前面所说,STL ...

  5. 洛谷 P1032 字串变换 题解

    每日一题 day19 打卡 Analysis 广搜+map判重 用find寻找字串,再用replace替换字串 这里的map相当于正常广搜的一个book的作用 #include<iostream ...

  6. 洛谷 P1032 字串变换(map)

    题目传送门 解题思路: 搜索题,因为要求最少次数,用bfs. AC代码: #include<cstdio> #include<iostream> #include<cst ...

  7. 集训作业 洛谷P1032 字串变换

    集训的题目有点多,先写困难的绿题吧(简单的应该想想就会了) 嗯,这个题看起来像个搜索呢(就是个搜索) 我们仔细想想就知道这个题肯定不能用深搜,可以优化的地方太少了,TLE是必然的. 那我们该怎么办呢? ...

  8. 洛谷P1032 字串变换-题解

    https://www.luogu.org/problemnew/show/P1032--(题目传送) 好在数据范围很小,暴力一点也能过.思路较简单,按照所有规则,从第一位开始广搜. 注意:1.str ...

  9. luogu题解P1032字串变换--BFS+STL:string骚操作

    题目链接 https://www.luogu.org/problemnew/show/P1032 分析 这题本来很裸的一个BFS,发现其中的字符串操作好烦啊.然后就翻大佬题解发现用STL中的strin ...

随机推荐

  1. Python操作redis系列之 列表(list) (五)

    # -*- coding: utf- -*- import redis r =redis.Redis(host=,password=") 1. Lpush 命令将一个或多个值插入到列表头部. ...

  2. 下载fiddler证书并设置信任

    一.苹果手机 待整理 二.android手机 待整理

  3. 如何查看Ubuntu下已安装包版本号

    之前的工作大部分在红帽企业版 下工作,查找安装包用rpm 很方便,但是由于这边大部分的服务器的运行环境是ubantu,补补. 在终端下也可以很方便查看已安装的软件包版本号,也能单独查看所需要的软件包是 ...

  4. MySQL 5.6新特性 -- crash-safe replication

    在slave上有两个线程:io线程和sql线程io线程接收master的二进制日志信息并写入到本地的relay log中:sql线程执行本地relay log中的信息.io线程读取到的二进制日志当前位 ...

  5. Fluent动网格【2】:Profile文件

    动网格中一个重要任务是部件运动方式的指定.在动网格中指定部件的运动,往往将部件的运动方式指定为其加速度.速度或位移与时间的相关关系,本文主要讲述如何在Fluent中利用瞬态Profile文件指定部件的 ...

  6. Blink

    https://help.aliyun.com/document_detail/66088.html?spm=a2c4g.11186623.6.602.58ff5686FP4Ihh

  7. MYSQL 线程池

    https://www.jianshu.com/p/88e606eca2a5 https://www.percona.com/doc/percona-server/LATEST/performance ...

  8. 每日英语:Don't Call Us Bossy

    [Confident girls are often called the other B-word, and it can keep them from reaching their full po ...

  9. 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求(转)

    通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需要获取认证信息(如Cookie),所以返回的是HttpWebRe ...

  10. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-5 底层驱动之旋转编码器

    源视频包下载地址:链接:http://pan.baidu.com/s/1mhENI9i密码:mf1x 银杏科技优酷视频发布区:http://i.youku.com/gingko8