洛谷 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 ...
随机推荐
- c++程序—选择结构
if(判断条件){执行语句} #include<iostream> using namespace std; #include<string> int main() { ; c ...
- ruby资料
源码样例 链接: https://pan.baidu.com/s/1mh55bFM 密码: 6cjy 初级代码 链接: https://pan.baidu.com/s/1hschnUW 密码: 8n1 ...
- NASA航天技术演示实验集
为了支持太空探索NASA开起了一系列先进太空技术演示实验. 具体的实验实验地址在:https://www.nasa.gov/mission_pages/tdm/main/overview.html 通 ...
- Python练习题3
1.九九乘法表 li = [1,2,3,4,5,6,7,8,9] for i in li: for j in li: if i >= j: print(i,'*',j,'=',i*j,end=& ...
- Oracle 查询当前用户下的所有表
select table_name from user_tables;
- 139-PHP static后期静态绑定(二)
<?php class test{ //创建test类 public function __construct(){ static::getinfo(); //后期静态绑定 } public s ...
- 4. Retrieving a mapper(检索映射器)
Retrieving a mapper(检索映射器) 4.1. The Mappers factory(映射工厂) 可以通过 org.mapstruct.factory.Mappers 类检索映射器实 ...
- springboot关闭whitelabel Error page
当访问不存在的页面时报错: 如何关闭它?有2种方法, 方法1: application.properties中加入server.error.whitelabel.enabled=false 方法2:在 ...
- 洛谷 P2458 [SDOI2006]保安站岗
题目传送门 解题思路: 树形DP 可知一个点被控制有且仅有一下三种情况: 1.被父亲节点上的保安控制 2.被儿子节点上的保安控制 3.被当前节点上的保安控制 我们设dp[0/1/2][u]表示u节点所 ...
- 【pwnable.kr】leg
pwnable从入门到放弃第八题. Download : http://pwnable.kr/bin/leg.cDownload : http://pwnable.kr/bin/leg.asm ssh ...