[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 ...
随机推荐
- 安卓学习之学生签到APP(一)
一.学生定位签到页面 具体实现步骤: 1.1 高德地图申请key 1.创建新应用 进入高德地图api控制台,创建一个新应用.如果您之前已经创建过应用,可直接跳过这个步骤. 2.添加新Key 在创建的应 ...
- 【Linux】tmpfs简介及增加方式
一. 什么是tmpfs? tmpfs是一种基于内存的文件系统,它和虚拟磁盘ramdisk比较类似,但不完全相同,和ramdisk一样,tmpfs可以使用RAM,但它也可以使用swap分区来存储.而且传 ...
- Deutsch lernen (08)
1. empfehlen - empfahl - hat empfohlen 推荐:劝说,劝告 Können Sie mir einen guten Artz empfehlen? jemand e ...
- vue.js的ajax和jsonp请求
首先要声明使用ajax 在 router下边的 Index.js中 import VueResource from 'vue-resource'; Vue.use(VueResource); ajax ...
- Asp.Net Core 自动适应Windows服务、Linux服务、手动启动时的内容路径的扩展方法
public static IWebHostBuilder UseContentRootAsEnv(this IWebHostBuilder hostBuilder) { bool IsWindows ...
- Hbuider sass配置 webstorm scss配置
--no-cache %FileName% ../css/%FileBaseName%.css sass编译后保存到css目录下 webstorm scss配置 C:\Ruby22-x64\bin ...
- 爬虫系列(六) 用urllib和re爬取百度贴吧
这篇文章我们将使用 urllib 和 re 模块爬取百度贴吧,并使用三种文件格式存储数据,下面先贴上最终的效果图 1.网页分析 (1)准备工作 首先我们使用 Chrome 浏览器打开 百度贴吧,在输入 ...
- d正则表达式相关
控件看Delphi正则表达式使用方法(TPerlRegEx)的介绍: 使用正则表达式获取Sql查询语句各项(表名.字段.条件.排序) 希望在线测试正则表达式 在线正则表达式测试 有许多常规表达式 希 ...
- 0802关于mysql数据库的锁机制
参考网址 http://www.cnblogs.com/yelbosh/p/5813865.html http://www.cnblogs.com/chenpingzhao/p/5065316.htm ...
- 【ACM】hdu_1095_A+BVII_201307261740
A+B for Input-Output Practice (VII)Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...