题目传送门

解题思路:

搜索题,因为要求最少次数,用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. win下的常用8个命令

    windows下常用的几个指令 一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的 ...

  2. es6 中的 Promise

    var promise = new Promise( function( resolve, reject ){             function onServiceSuccess( data ...

  3. CocosCreator - 向上传递事件(冒泡)

    /** *   分发事件到事件流中. *   this.node.dispatchEvent(new cc.Event.EventCustom("name",是否向上传递)) *  ...

  4. 指令——df

    df是disk free 的简称,这个指令的功能和作用是查看磁盘空间. 可以加上 -h 的选项,来提高可读性. [he@localhost ~]$ df -h文件系统(磁盘名称)      总容量  ...

  5. 五十五、SAP中调用系统自带的函数

    一.我们需要取一个月中的最后一天,代码如下 二.执行结果如下 三.以上为纯手打,错了好几次才改过来,还有一个办法就是系统自动生成,点击编辑->模式 四.输入需要调用的函数名字BKK_GET_MO ...

  6. 联系我们地图坐标展示js

    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6d88 ...

  7. ubuntu在虚拟机下的安装 ~~~ Hadoop的安装及配置 ~~~ Hdfs中eclipse的安装

     前言 Hadoop是基于Java语言开发的,具有很好跨平台的特性.Hadoop的所要求系统环境适用于Windows,Linux,Mac系统,我们推荐选择使用Linux或Mac系统.而Linux系统则 ...

  8. python 列表和字符串

    python 列表中保留所有字符串前三项,并保存到一个新的列表l = [s[:3] for s in data] python 在列表中查找包含所以某个字符串的项,并保存到一个新的列表l = [s f ...

  9. C++基础--引用的一点补充

    这一篇是对引用的一点补充,内容基本上是来自<C++ primer plus>一书第八章的内容. 前面一篇介绍了引用的一点特点,这里补充一个,将引用用于类对象的时候,有一个体现继承的特征,就 ...

  10. storm on yarn安装时 提交到yarn失败 failed

    最近在部署storm on yarn ,部署参考文章 http://www.tuicool.com/articles/BFr2Yvhttp://blog.csdn.net/jiushuai/artic ...