【搜索】P1032 字串变换
题目描述
已知有两个字串A,B及一组字串变换的规则(至多6个规则):
A1 ->B1
A2 -> B2
规则的含义为:在 A中的子串 A1 可以变换为B1,A2 可以变换为 B2 …。
例如:A='abcd'B='xyz'
变换规则为:
‘abc’->‘xu’‘udud’->‘y’‘y’->‘yz’
则此时,A可以经过一系列的变换变为B,其变换的过程为:
‘abcd’->‘xud’->‘xy’->‘xyz’
共进行了3次变换,使得A变换为B。
输入输出格式
输入格式:
输入格式如下:
A B
A1 B1
A2 B2 |-> 变换规则
... ... /
所有字符串长度的上限为20。
输出格式:
输出至屏幕。格式如下:
若在10步(包含10步)以内能将A变换为B,则输出最少的变换步数;否则输出"NO ANSWER!"
输入输出样例
abcd xyz
abc xu
ud y
y yz
3
#include<bits/stdc++.h>
using namespace std;
const int N = ;
string A,B,a[N],b[N];
map<string,int>Mp; typedef struct Node{
string S;
int step;
}Node; int cnt = ;
int Check(string u,string v,int pos = ){
int L1 = u.length() ;
int L2 = v.length() ;
int i = pos , j = ;
while( i < L1 && j < L2 ){
while( u[i] == v[j] ){
i++;
j++;
if( j == L2 ){
return i-L2;
}
}
j = ;
i ++ ;
}
return -;
}
int ans = - ;
void bfs( ){ queue<Node>Q;
Q.push ( Node{A,} );
Mp[A] = ;
while ( !Q.empty() ){
Node cur = Q.front();
Q.pop();
if( cur.step >= ) continue;
if( cur.S == B ){
ans = cur.step;
return ;
}
for(int i=;i<cnt;i++){
string tmp = cur.S;
int Len = tmp.length();
int L = a[i].length();
for( int j= ; j < Len ; j++ ) {
int pos = Check(tmp,a[i],j);
if( pos != - && pos != ){
string s1 = tmp.substr(,pos);
string s2 = b[i];
string s3 = tmp.substr(pos+L);
string t = s1+s2+s3;
//cout<<t<<endl;
if( Mp[t] == ){
Mp[t] = ;
Q.push(Node{t,cur.step+});
}
}
if( pos == ){
string s1 = b[i];
string s2 = tmp.substr(pos+L);
string t = s1 + s2 ;
//cout<<t<<endl;
if( Mp[t] == ){
Mp[t] = ;
Q.push(Node{t,cur.step+});
}
}
}
}
}
}
int main() { ios_base :: sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL); cin>>A>>B; while( cin>>a[cnt]>>b[cnt] ){ if( a[cnt] == "" && b[cnt]=="")
break;
cnt++;
}
bfs();
if(ans==-){
puts("NO ANSWER!");
}else{
printf("%d\n",ans);
}
return ;
}
【搜索】P1032 字串变换的更多相关文章
- P1032 字串变换 字符串BFS
题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1 ->B_1B1 A_2A2 -> B_2B2 规则的含义为:在 AA中的子串 A_1A1 ...
- 洛谷 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] 字串变换
洛谷题目链接:字串变换 题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B ...
- luogu P1032 字串变换
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...
- [NOIP2002] 提高组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 字串变换
题目地址:https://www.luogu.org/problemnew/show/P1032 洛谷训练场BFS的训练题呀. “BFS不就是用队列的思想去遍历一切情况嘛.我已经不是小孩子了,我肯定能 ...
随机推荐
- Gearman1.1.12安装与启动
1)安装 a)安装gcc4.4环境: i. yum install gcc44 gcc44-c++ libstdc++44-devel gcc-c++ -y ii. 在/etc/profile中添加 ...
- File System Programming---(三)
Accessing Files and Directories Before you can open a file, you first have to locate it in the file ...
- (二十五)后台开发-分类信息的curd -展示所有实现
案例1-分类信息的curd 步骤分析: 左边的dtree: 1.导入dtree.js 2.导入dtree.css 3.创建一个div 添加样式 class="dtree" 4.在d ...
- checkbox设置单选的的两种方式
一.如果 <input name="ck" type="checkbox">是页面加载就有的 $("#input[name=ck]&quo ...
- 安装GitLab出现ruby_block[supervise_redis_sleep] action run
在卸载gitlab然后再次安装执行sudo gitlab-ctl reconfigure的时候往往会出现:ruby_block[supervise_redis_sleep] action run,会一 ...
- 如何实现session的共享?
1.以cookie加密的方式保存在客户端. 优点是减轻服务器端的压力 缺点是受到cookie的大小限制,可能占用一定带宽,因为每次请求会在头部附带一定大小的cookie信息,另外这种方式在用户禁止使用 ...
- Cg profiles,如何使用CGC编译Cg语言(转)
抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 计算机只能理解和执行由0.1序列(电压序列 ...
- bzoj 5498: [2019省队联测]皮配【dp】
是个神仙dp-- 参考:https://www.luogu.org/blog/xzz-233/solution-p5289 设f[i][j][k]是前i个有限制的城市,所有学校中选蓝色阵营有j人,有限 ...
- Codeforces 1000 (A~E)
A Codehorses T-shirts 相同长度之间互相转化即可 #include<iostream> #include<cstdio> #include<cstri ...
- js中的预解释
在js中,带var 和function关键字的需要预解释: 那什么是预解释?就是在js代码执行之前,先申明好带有var 关键字和带有function关键字的变量,在内存里先安排好.但是带有var关键字 ...