题目描述

已知有两个字串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. label标签的可访问性

    与表单元素关联的方法(IE6下label标签包裹控件的方法是不顶用的): 1 使用for和id关联控件 <p><label for="test">标签< ...

  2. class.forName()和.class有什么区别?

    class.forName()会初始化类的成员(静态的),最先加载的是类的静态成员变量,然后是静态代码块. 访问常量并不会导致类的初始化,但是访问静态成员会.

  3. 023--python os、sys、json、pickle、xml模块

    一.os模块 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 >>> os.getcwd() 'C:\\Python36' os.chdir(&quo ...

  4. 简析hotjar录屏功能实现原理

    简析hotjar录屏功能实现原理 众所周知,hotjar中录屏功能是其重要的一个卖点,看着很牛X酷炫的样子,今天就简单的分析一下其可能实现(这里只根据其请求加上个人理解分析,并不代表hotjar中真实 ...

  5. loj#2540. 「PKUWC2018」随机算法

    传送门 完了pkuwc咋全是dp怕是要爆零了-- 设\(f(S)\)表示\(S\)的排列数,\(S\)为不能再选的点集(也就是选到独立集里的点和与他们相邻的点),\(mx(S)\)表示\(S\)状态下 ...

  6. php 获得上周数据

    $lastMondy = date('Y-m-d', strtotime('-2 sunday +1 days', time()));$lastSundy = date('Y-m-d', strtot ...

  7. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

  8. 修改dns访问android.com

    1.几个常用dns服务器 8.8.8.8 美国 加利福尼亚州圣克拉拉县山景市谷歌公司DNS服务器 8.8.4.4 美国 加利福尼亚州圣克拉拉县山景市谷歌公司DNS服务器 8.8.4.3 美国 加利福尼 ...

  9. android动画(3)layout动画,layoutChanged动画及算定义它,ListViewActivity的Layout动画(代码和xm配置两种实现l)

    1.layout切换动画 代码: 本示例是fragment切换.在它的oncreateView中 public class LayoutAnimationFrgmt extends Fragment ...

  10. httpclient 3.1跳过https请求SSL的验证

    一.因为在使用https发送请求的时候会涉及,验证方式.但是这种方式在使用的时候很不方便.特别是在请求外部接口的时候,所以这我写了一个跳过验证的方式.(供参考) 二.加入包,这里用的是commons- ...