洛谷P1032 字串变换【bfs】
题目链接: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】的更多相关文章
- [洛谷P1032] 字串变换
洛谷题目链接:字串变换 题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B ...
- 洛谷 P1032 字串变换题解
题目链接:https://www.luogu.org/problem/P1032 题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1 ->B_1B1 A ...
- 洛谷 P1032 字串变换
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...
- 洛谷 P1032 字串变换 (BFS)
题目传送门 我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出 STL大法好 这题最麻烦的其实是处理字符串,真正的搜索部分我个人认为也就只有橙题或黄题的难度.而处理字符串,正如前面所说,STL ...
- 洛谷 P1032 字串变换 题解
每日一题 day19 打卡 Analysis 广搜+map判重 用find寻找字串,再用replace替换字串 这里的map相当于正常广搜的一个book的作用 #include<iostream ...
- 洛谷 P1032 字串变换(map)
题目传送门 解题思路: 搜索题,因为要求最少次数,用bfs. AC代码: #include<cstdio> #include<iostream> #include<cst ...
- 集训作业 洛谷P1032 字串变换
集训的题目有点多,先写困难的绿题吧(简单的应该想想就会了) 嗯,这个题看起来像个搜索呢(就是个搜索) 我们仔细想想就知道这个题肯定不能用深搜,可以优化的地方太少了,TLE是必然的. 那我们该怎么办呢? ...
- 洛谷P1032 字串变换-题解
https://www.luogu.org/problemnew/show/P1032--(题目传送) 好在数据范围很小,暴力一点也能过.思路较简单,按照所有规则,从第一位开始广搜. 注意:1.str ...
- luogu题解P1032字串变换--BFS+STL:string骚操作
题目链接 https://www.luogu.org/problemnew/show/P1032 分析 这题本来很裸的一个BFS,发现其中的字符串操作好烦啊.然后就翻大佬题解发现用STL中的strin ...
随机推荐
- What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?
来自:http://stackoverflow.com/questions/510632/whats-the-difference-between-concurrenthashmap-and-coll ...
- 视音频数据处理入门:AAC音频码流解析
===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...
- [k8s]coredns/kube-dns配置subdomain
思想: kube-dns或coredns本质上是一个dns服务软件.都需要配置配置文件.要控制怎么查询,即控制他的配置文件即可. 本文先说下coredns怎么配置,然后在配下kube-dns(包含了外 ...
- 远程桌面中Tab键不能补全的解决办法
我们曾在之前的一篇文章中介绍了windows远程连接ubuntu的方法,在成功登陆远程桌面环境之后,发现在终端中Tab键不能自动补齐(但是Ctrl +Tab 可以用,但是需要按下组合键才能补全的话,时 ...
- Asp.Net 隐藏手机号中间四位为*方法
需求:15088881234 > 150****1234 方法1: "; , ) + , ); 方法2: "; string p2= Regex.Replace(phone ...
- 用Jmeter+Badboy+Fiddler做接口测试
用Jmeter+Badboy+Fiddler做接口测试 2016-12-05 目录: 1 简介2 Badboy录制3 Jmeter打开Badboy脚本4 用Fiddler抓请求,补充完善脚本5 测试中 ...
- 登录tomcat服务器首页直接跳转到项目
原文:https://www.cnblogs.com/xwdreamer/p/3489996.html 需求: 客户觉得每次输入http://10.138.16.232:8080/abc/ 比较烦,他 ...
- Repeater数据控件的两个重要事件ItemDataBound 和 ItemCommand
1 ItemDataBound:数据绑定的时候(正在进行时)发生. 2 ItemCommand :用来响应Item模板中的控件的事件. 如下代码 aspx代码: [html] view plain c ...
- 能ping通外网的域名,浏览器不能上网的解决办法
1,依次尝试了关闭防火墙,关闭杀毒软件,手动设置DNS都没有用. 2,最后通过这个cmd命令搞定,特此记录一下,重置初始化网络环境. netsh winsock reset 补充,上面的命令,重启电脑 ...
- Vue.js常用指令:v-on
一.v-on指令 v-on指令在Vue.js中用来处理对应的事件.用法: v-on:事件类型="函数体" 例如:点击按钮的时候执行play事件 <button v-on:cl ...