洛谷 P1032 字串变换(map)
题目传送门
解题思路:
搜索题,因为要求最少次数,用bfs.
AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map> using namespace std; string a,b,ca[],cb[];
int l;
queue<string> q;
queue<int> q1;
map<string,bool> aa; inline void _read() {
cin >> a >> b;
for(int i = ;i <= ; i++)
cin >> ca[i] >> cb[i];
l = ;
while(ca[l][] == '\0') l--;
if (l == && a != b) {
cout << "NO ANSWER!";
return ;
}
q.push(a);
q1.push();
} inline void bfs() {
string pp,oo;
int m = ;
while(!q.empty() && q1.front() <= && q.front()!=b) {
pp = q.front();
if(aa[pp]) {
q.pop();
q1.pop();
continue;
}
aa[pp] = ;
for(int i = ;i <= l; i++) {
while(true) {
m = pp.find(ca[i]);
if(m == -) break;
oo = pp;
oo.replace(m,ca[i].size(),cb[i]);
q.push(oo);
q1.push(q1.front() + );
pp[m] = '%';
}
}
q.pop();
q1.pop();
}
} inline void panduan_and_print() {
if(q1.front() > ) {
printf("NO ANSWER!");
return ;
}
if(q.empty()) {
printf("NO ANSWER!");
return ;
}
printf("%d",q1.front());
} int main()
{
_read();
bfs();
panduan_and_print();
return ;
}
40分
//一开始40分,错误在bfs过程中oo和pp的值处理不对
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map> using namespace std; string a,b,ca[],cb[];
int l;
queue<string> q;
queue<int> q1;
map<string,int> aa; inline void _read() {
cin >> a >> b;
for(int i = ;i <= ; i++)
cin >> ca[i] >> cb[i];
l = ;
while(ca[l][] == '\0') l--;
if (l == && a != b) {
cout << "NO ANSWER!";
return ;
}
q.push(a);
q1.push();
} inline void bfs() {
string pp,oo;
int m = ;
while(!q.empty() && q1.front() <= && q.front()!=b) {
if(aa[q.front()] == ) {
q.pop();
q1.pop();
continue;
}
aa[q.front()] = ;
for(int i = ;i <= l; i++) {
pp = q.front();
while(true) {
m = pp.find(ca[i]);
if(m == -) break;
oo = q.front();
oo.replace(m,ca[i].size(),cb[i]);
q.push(oo);
q1.push(q1.front() + );
pp[m] = '~';//将S里子串sa[i]的第一次出现位置随便换成另一种无关的字符,这样就可以查找到S里子串sa[i]的下一个出现位置
}
}
q.pop();
q1.pop();
}
} inline void panduan_and_print() {
if(q1.front() > ) {
printf("NO ANSWER!");
return ;
}
if(q.empty()) {
printf("NO ANSWER!");
return ;
}
printf("%d",q1.front());
} int main()
{
_read();
bfs();
panduan_and_print();
return ;
}
100分
洛谷 P1032 字串变换(map)的更多相关文章
- [洛谷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 字串变换【bfs】
题目链接:https://www.luogu.org/problemnew/show/P1032 题意: 给定一个原字符串和目标字符串,以及几个字符串变换的规则. 问能否根据这几个规则在十步之内把原字 ...
- 洛谷P1032 字串变换-题解
https://www.luogu.org/problemnew/show/P1032--(题目传送) 好在数据范围很小,暴力一点也能过.思路较简单,按照所有规则,从第一位开始广搜. 注意:1.str ...
- 集训作业 洛谷P1032 字串变换
集训的题目有点多,先写困难的绿题吧(简单的应该想想就会了) 嗯,这个题看起来像个搜索呢(就是个搜索) 我们仔细想想就知道这个题肯定不能用深搜,可以优化的地方太少了,TLE是必然的. 那我们该怎么办呢? ...
- P1032 字串变换 字符串BFS
题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1 ->B_1B1 A_2A2 -> B_2B2 规则的含义为:在 AA中的子串 A_1A1 ...
随机推荐
- 【BZOJ4237】稻草人
题意 给定平面上 \(N\) 个关键点,询问有多少个矩形满足左下和右上各有一个关键点,且矩形中间没有关键点. \(N\le 2\cdot 10^5\) . 题解 我们按 \(x\) 排序分治,对于左右 ...
- delphi dll编写与调用
dll代码: mydll.dpr library mydll; uses System.SysUtils, System.Classes, uFunction in 'uFunction.pas'; ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-text-width
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- spring源码 继承AttributeAccessor的BeanDefinition接口
/** * A BeanDefinition describes a bean instance, which has property values, * constructor argument ...
- 第十四篇Django-model进阶(中介模型,查询优化,extra,整体插入)
Django-model进阶(中介模型,查询优化,extra,整体插入) 阅读目录(Content) 中介模型 查询优化 extra 整体插入 中介模型 处理类似搭配 pizza 和 topping ...
- 初步了解URL
URl的定义: 在webs上每种可用的资源(比如:HTML文档,图像,视频片段,程序等)都可以由一个通用的资源标志符(Universal Resource Identifier)进行定位.URl的组成 ...
- 一、SAP中添加一个模块到收藏夹后,显示事务代码
一.在SAP中,如果添加一个模块到收藏夹,默认是看不到事务代码的,如图: 二.我们在附件->设置中勾选显示技术名称 三.保存之后,就会显示出事务代码,如图所示: 不忘初心,如果您认为这篇文章有价 ...
- jquery判断当前浏览器是否是IE
if (window.ActiveXObject || "ActiveXObject" in window){ layer.msg("This page does not ...
- 标准JAVA工程结构
- nginx安装出现:cp: `conf/koi-win' and `/application/nginx-1.6.3/conf/koi-win' are the same file
nginx编译安装时make出现如下错误 ]: Leaving directory `/application/nginx-' make -f objs/Makefile install ]: Ent ...