[luogu普及] ---P1032 字串变换
目的
- 刷完100AC (最近很不舒服,写博客耗时啊
- 记录第一个字符串的题目
参考
https://www.luogu.org/blog/user20197/solution-p1032
代码
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<iostream>
#define MAX 20+9
using namespace std;
struct node{
string s;
int dis;
};
string original[MAX];
string translate[MAX];
int n;
map <string, int> m;//判重
string x,y;
int ans;
string trans(const string &str, int i, int j)//在str的第i个位置使用第j种手法改变
{
string ans = "";
if(i+original[j].length() > str.length() ) return ans;//???有必要?
for(int k = 0; k < original[j].length() ; k++)
if(str[i+k] != original[j][k]) return ans;//判断是否可以变换
ans = str.substr(0, i);//逐个拼接
ans += translate[j];
//ans += str.substr(i+original[j].length());
ans += str.substr(i+original[j].length() , str.length() );
return ans;
}
void bfs() {
queue <node> q;
node str;
str.s = x;
str.dis = 0;
q.push(str);
while(!q.empty() ) {
node u = q.front() ;
q.pop() ;
if(m[u.s ]) continue;
if(u.s == y) {
ans = u.dis ;
break ;
}
m[u.s ] = 1;
string tmp;
for(int i = 0; i < u.s.length() ; i++) {
for(int j = 0; j < n; j++) {
tmp = trans(u.s , i, j);
if(tmp != "") {
node e;
e.s = tmp;
e.dis = u.dis +1;
q.push(e);
}
}
}
}
if (ans > 10 || ans == 0)// 不懂这题目,为啥0步不行
cout << "NO ANSWER!" << endl;
else
cout << ans << endl;
}
int main() {
cin>>x>>y;
while(cin>>original[n]>>translate[n]) n++;
bfs();
return 0;
}
[luogu普及] ---P1032 字串变换的更多相关文章
- luogu题解P1032字串变换--BFS+STL:string骚操作
题目链接 https://www.luogu.org/problemnew/show/P1032 分析 这题本来很裸的一个BFS,发现其中的字符串操作好烦啊.然后就翻大佬题解发现用STL中的strin ...
- luogu P1032 字串变换
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 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
题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1 ->B_1B1 A_2A2 -> B_2B2 规则的含义为:在 AA中的子串 A_1A1 ...
- [洛谷P1032] 字串变换
洛谷题目链接:字串变换 题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B ...
- 【搜索】P1032 字串变换
题目描述 已知有两个字串A,B及一组字串变换的规则(至多6个规则): A1 ->B1 A2 -> B2 规则的含义为:在 A中的子串 A1 可以变换为B1,A2 可以变换为 ...
- [NOIP2002] 提高组P1032 字串变换
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...
- 洛谷 P1032 字串变换 (BFS)
题目传送门 我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出 STL大法好 这题最麻烦的其实是处理字符串,真正的搜索部分我个人认为也就只有橙题或黄题的难度.而处理字符串,正如前面所说,STL ...
随机推荐
- 如何用js代码实现图片切换效果
通过点击按钮,实现图片的隐藏与显现,切换. 实现代码:<style> .a{ width: 300px; height: 300px; border: 1px solid black; } ...
- java中的位运算及移位运算
为了方便对二进制位进行操作,Java给我们提供了以下四个二进制位操作符: & 按位与 | 按位或 ^ 按位异或 ~ 按位取反 Java中有三个移位运算符: 左移:&l ...
- 【sqli-labs】 less8 GET - Blind - Boolian Based - Single Quotes (基于布尔的单引号GET盲注)
加单引号 没有任何信息输出 加and 页面变化,不正常是没有任何回显 http://localhost/sqli/Less-8/?id=1' and '1'='1 http://localhost/s ...
- 腾讯模板引擎template
template.js是一款JavaScript模板引擎,用来渲染页面的. 原理:提前将Html代码放进编写模板 script id="tpl" type="text/ ...
- HTML5 video常用属性
一.视频video常用标签方法 <!-- controls 控制条,播放暂停等 controlslist 控制不允许全屏 不允许下载等 poster 封面 autoplay 自动播放 muted ...
- Hadoop分布式文件系统架构部署
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://wgkgood.blog.51cto.com/1192594/1332340 前言 ...
- XX-Net的完整教程
1.下载谷歌浏览器,安装. 2.百度搜索github,github中搜索XX-Net,下载稳定版 3.解压缩下载的文件夹,运行start.vbs文件.如果弹出管理员请求权限请允许,弹出防火墙警告,请允 ...
- 63.es中的type数据类型
主要知识点 理解es中的type数据类型 一.type的理解 type是一个index中用来区分类似的数据的,但是可能有不同的fields,而且有不同的属性来控制索引建立.分词器.field的 ...
- str使用注意点(未完待续)
1.(切片)str[num1,num2],是指截取 str[num1] 到 str[num2-1] 直接的元素
- 14.Idea在html里面的快捷键
1.IntelliJ-Idea里常用的快捷键 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*…*/ ) Shift+F6 重构-重命名 Ctrl+X 删除行 Ctrl+D 复制行 C ...