题目描述

已知有两个字串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!"

输入输出样例

输入样例#1:

abcd xyz
abc xu
ud y
y yz
输出样例#1:

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

  1. P1032 字串变换 字符串BFS

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

  2. 洛谷 P1032 字串变换题解

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

  3. 洛谷 P1032 字串变换

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

  4. [洛谷P1032] 字串变换

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

  5. luogu P1032 字串变换

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

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

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

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

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

  8. 洛谷 P1032 字串变换 题解

    每日一题 day19 打卡 Analysis 广搜+map判重 用find寻找字串,再用replace替换字串 这里的map相当于正常广搜的一个book的作用 #include<iostream ...

  9. 【洛谷】P1032 字串变换

    题目地址:https://www.luogu.org/problemnew/show/P1032 洛谷训练场BFS的训练题呀. “BFS不就是用队列的思想去遍历一切情况嘛.我已经不是小孩子了,我肯定能 ...

随机推荐

  1. 【旧文章搬运】修改PEB,断链隐藏模块成功

    原文发表于百度空间,2008-7-26========================================================================== 继续实践之前 ...

  2. UVaLive 7456 Least Crucial Node (并查集+暴力 或者 求割点)

    题意:求标号最小的最大割点.(删除该点后,指定点#sink能到达的点数减少最多). 析:由于不知道要去掉哪个结点,又因为只有100个结点,所以我们考虑用一个暴力,把所有的结点都去一次,然后用并查集去判 ...

  3. SpringIOC 二—— 容器 和 Bean的深入理解

    上文:Spring IOC 一--容器装配Bean的简单使用 上篇文章介绍了 Spring IOC 中最重要的两个概念--容器和Bean,以及如何使用 Spring 容器装配Bean.本文接着记录 S ...

  4. 【水水水】678A - Johny Likes Numbers

    #include<stdio.h> #include<iostream> #include<cstdio> #include<queue> #inclu ...

  5. poj 2960 S-Nim【SG函数】

    预处理出SG函数,然后像普通nim一样做即可 #include<iostream> #include<cstdio> using namespace std; const in ...

  6. P1648 看守

    传送门 以二维的两个点\((x1,y1),(x2,y2)\)为例,那么他们之间的曼哈顿距离肯定为一下四个之一\((x1-x2)+(y1-y2)\),\((x2-x1)+(y1-y2)\),\((x1- ...

  7. 屏蔽QQ黄钻官方团队送礼物的方法

    按照在网上查到的方法: 登录手机QQ \(\longrightarrow\) 好友动态 \(\longrightarrow\) 个人主页 \(\longrightarrow\) 右上角三道杠 \(\l ...

  8. Luogu P1156 垃圾陷阱 【dp】By cellur925

    题目传送门 这题...看上去浓浓的背包气息...但是并不好设计状态啊emmm. 我们考虑可能成为状态的量:高度.血量.时间.物品.看数据范围也猜到应该大概是个二维dp了w. 正确的状态设计之一:设$f ...

  9. maven groupid与artifactid

    groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找. groupId一般分为多个段 ...

  10. 清橙A1339. JZPLCM(顾昱洲)

    http://www.tsinsen.com/ViewGProblem.page?gpid=A1339 题解:https://blog.csdn.net/LOI_DQS/article/details ...