目的

  1. 刷完100AC (最近很不舒服,写博客耗时啊
  2. 记录第一个字符串的题目

参考

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 字串变换的更多相关文章

  1. luogu题解P1032字串变换--BFS+STL:string骚操作

    题目链接 https://www.luogu.org/problemnew/show/P1032 分析 这题本来很裸的一个BFS,发现其中的字符串操作好烦啊.然后就翻大佬题解发现用STL中的strin ...

  2. luogu P1032 字串变换

    题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...

  3. 洛谷 P1032 字串变换题解

    题目链接:https://www.luogu.org/problem/P1032 题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1​ ->B_1B1​ A ...

  4. 洛谷 P1032 字串变换

    题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...

  5. P1032 字串变换 字符串BFS

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

  6. [洛谷P1032] 字串变换

    洛谷题目链接:字串变换 题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B ...

  7. 【搜索】P1032 字串变换

    题目描述 已知有两个字串A,B及一组字串变换的规则(至多6个规则): A1​ ->B1​ A2​ -> B2​ 规则的含义为:在 A中的子串 A1​ 可以变换为B1​,A2​ 可以变换为  ...

  8. [NOIP2002] 提高组P1032 字串变换

    题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...

  9. 洛谷 P1032 字串变换 (BFS)

    题目传送门 我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出 STL大法好 这题最麻烦的其实是处理字符串,真正的搜索部分我个人认为也就只有橙题或黄题的难度.而处理字符串,正如前面所说,STL ...

随机推荐

  1. oracle 提示登录密码过期解决

    1.登录到oracle的 服务器 2.切换到oracle 用户 3.设置到当前操作的实例名:export ORACLE_SID=XXX 4.连接数据库的命令行模式:sqlplus /nolog 5.s ...

  2. 获取 PHPstorm编辑器 注册码地址

    网址: http://idea.lanyus.com/ 注册码有效期为2016年11月24日至2017年11月23日使用前请将“0.0.0.0 account.jetbrains.com”添加到hos ...

  3. MFC和OpenCV结合

    最重要的一点:如何把OpenCV的框嵌入MFC的ID.. 把cv显示框嵌入 pic控件 cvNamedWindow("kalman"); HWND hWnd = (HWND) cv ...

  4. react基础篇六

    创建 Refs 使用 React.createRef() 创建 refs,通过 ref 属性来获得 React 元素.当构造组件时,refs 通常被赋值给实例的一个属性,这样你可以在组件中任意一处使用 ...

  5. [实战经验][SQL Sever 2008 (R)解决方法累积

    SQL Sever 2008 (R)的安装图解及配置 http://www.soft6.com/v9/2009/jcsj_1030/115821.html 产品密钥,选择“输入产品密钥”,输入:PTT ...

  6. react工具库

    采用了react框架后,需要找到一些常用的库,常见的需求比如: 1)react生成二维码 2)react的轮播banner图 随着react的社区的壮大,以上的需求都有专门的库帮我们做这个: 1)re ...

  7. JDK8新特性:Lambda表达式

    Lambda表达式,案例一:new Thread(() -> System.out.println("thread")); Lambda表达式,案例二:由参数/箭头和主体组成 ...

  8. inherit 关键字使得元素获取其父元素的计算值

    它可以应用于任何CSS属性,包括CSS简写 all. 对于继承属性,inherit 关键字只是增强了属性的默认行为,只有在重载(overload)其它规则的时候被使用.对于非继承属性,inherit ...

  9. loadrunner报错总结

    1.报错   没有缓存空间可用   TCP超时释放时间?是解决刚才那个报错的? 解决方法如下  修改TcpTimedWaitDelay值为1和MaxUserPort值为65534.最后,重启!  完美 ...

  10. 使用tomcat搭建Jenkins环境(centos7.3)

    1.从官网下载最新版本的tomcat下载地址:https://tomcat.apache.org/2.Jenkins 官方网站下载最新版本war包Jenkins官网地址:http://jenkins- ...