洛谷 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 ...
随机推荐
- 洛谷P1002——过河卒
又是洛谷题,要不是有小姐姐不会,我才不想动脑子.先贴一下题目地址https://www.luogu.org/problem/P1002 再贴一下题目: 我们读一下题目,这可不比学校的**算法题,读完一 ...
- MSDN原版系统镜像ISO下载站
官网 网址1:http://www.imsdn.cn/ Windows 10 最新版本 1909 正式版 ISO 镜像下载 (微软 MSDN / VL 官方原版系统) 网址2:https://www. ...
- centos7上安装mysql8
话不多说仍然是更换虚拟机的系列安装. 一.首先下载最先版的mysql.到官网下载https://dev.mysql.com/downloads/file/?id=477146 下载后上传linux到相 ...
- 【pwnable.kr】blackjack
又一道pwnable nc pwnable.kr 9009 读题找到源代码在:http://cboard.cprogramming.com/c-programming/114023-simple-bl ...
- Python基础笔记:list和tuple
list 与 tuple 就类似于C语言中的数组,list 与 tuple 的区别就是list 可修改,而tuple不可修改: list用法示例: >>> s=[] >> ...
- 学术Essay写作如何体现逻辑的应用
作为一篇学术essay,逻辑要求是必不可少的.那么,学术essay如何写作才能体现逻辑呢?这就需要从语言逻辑和科学逻辑出发. 语言逻辑指的是三C原则:(1)complete(完整),(2)concis ...
- 我们是如何将 ToB 服务的交付能力优化 75%?
ToB 服务交付的方式分为公有云部署和私有化部署两种.其中,对成本敏感的中小企业往往采用公有云部署的方式,从而尽量减少成本.客单价较高的大型企业.政府.银行和事业单位,考虑到数据隐私.安全.合规等要求 ...
- Web基础之Mybatis
Web基础之Mybatis 对比JdbcTempalte,mybatis才能称得上是框架,JdbcTempalte顶多算是工具类,同时,对比Hibernate,Mybatis又有更多的灵活性,算是 ...
- HDU - 1114 Piggy-Bank(完全背包讲解)
题意:背包重量为F-E,有N种硬币,价值为Pi,重量为Wi,硬币个数enough(无穷多个),问若要将背包完全塞满,最少需要多少钱,若塞不满输出“This is impossible.”. 分析:完全 ...
- idea将web项目打成war包放在tomcat/webapps上运行
1.进入Project Structure 或者 file -> Project Structure 或者 快捷键ctrl+alt+shift+s 2.选中Artifacts 3.点加号,然后如 ...