题目传送门

解题思路:

搜索题,因为要求最少次数,用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)的更多相关文章

  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 字串变换【bfs】

    题目链接:https://www.luogu.org/problemnew/show/P1032 题意: 给定一个原字符串和目标字符串,以及几个字符串变换的规则. 问能否根据这几个规则在十步之内把原字 ...

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

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

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

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

  9. P1032 字串变换 字符串BFS

    题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1​ ->B_1B1​ A_2A2​ -> B_2B2​ 规则的含义为:在 AA中的子串 A_1A1​ ...

随机推荐

  1. JD-Store购物网站复盘——20170312

    一.商店技术架构 1.主题 2.涉及技术点: 3.核心业务功能 4.角色 5.用户故事 二.实现步骤 专案基础设施 上传图片模块 购物车 订单 支付&寄信 专案源码 三.第三方服务应用 支付 ...

  2. usb摄像头驱动的移植

    相关软件下载地址:http://pan.baidu.com/s/16yo8Y 1.使用摄像头型号ov9650 ①修改.配置内核 1.修改vi drivers/i2c/busses/Kconfig (参 ...

  3. 概率图模型之EM算法

    一.EM算法概述 EM算法(Expectation Maximization Algorithm,期望极大算法)是一种迭代算法,用于求解含有隐变量的概率模型参数的极大似然估计(MLE)或极大后验概率估 ...

  4. P-数学程序猿今天开始写博客了

    ∧          /| /\7          ≤_/      ∧. |      |         /   /      /        〉 |     Z_,<   /      ...

  5. 021-PHP常用的数值类型判断函数

    <?php //判断数组 $colors = array("red", "blue", "green"); if(is_array($ ...

  6. 087-把PHP数组中的元素按随机顺序重新排列shuffle

    <?php $arr=array(3,23,'A','f','123','hello'); //定义一个数组 echo '排序之前的数组信息:<br>'; print_r($arr) ...

  7. ZOJ 1409 communication system 双变量型的DP

    这个题目一开始不知道如何下手,感觉很像背包,里面有两个变量,一个带宽B,一个价格P,有n个设备,每个设备有k个可选的器材(只需选一个),每个器材都有自己的B和P, n个设备选n个器材,最终,FB=所有 ...

  8. oracle中判断"非"

    在oracle中判断为"非"最常见的两种情况,一个是"不等于",一个的"非空". 通过查找资料得知,oracle中判断不等于的方法有好多种: ...

  9. 201771010142-张燕 实验一 软件工程准备—<软件工程的初步了解和学习目标>

    实验一 软件工程准备 项目 内容 软件工程 https://www.cnblogs.com/nwnu-daizh/ 软件工程准备要求 https://www.cnblogs.com/nwnu-daiz ...

  10. (排序EX)P1583 魔法照片

    题解: 需要注意的是,快排完之后并不是按照编号从小到大的顺序输出 #include<iostream>using namespace std;int r=0;void swap(int & ...