题目描述

已知有两个字串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. VS2008 视图资源.rc无法加载的问题及解决方法

    VS2008 视图资源.rc无法加载 1.首先先把vs关闭,然后执行 开始>>所有程序>>Mircosoft visual studio 2008>>visual ...

  2. Table View Programming Guide for iOS---(五)---Creating and Configuring a Table View

    Creating and Configuring a Table View Your app must present a table view to users before it can mana ...

  3. File System Programming --- (二)

    File System Basics The file systems in OS X and iOS handle the persistent storage of data files, app ...

  4. mysql连接过多-报错

    有两种方法: 1,错误连接参数 max_connect_errors,在配置文件中调整增大. 比如:修改mysql配置文件,在[mysqld]字段下面添加 max_connect_errors=102 ...

  5. display:inline-block的div 与 display:block的div之间有间隔问题(div与div之间有间隔的可能性)

    首先看一下我出现的问题如下图: 如上图所示,我的导航栏是由三部分组成的,三部分样式如下: .logo{ /*红框*/ position: relative; display: inline-block ...

  6. (水题)洛谷 - P1603 - 斯诺登的密码

    https://www.luogu.org/problemnew/show/P1603 有毒,大小写不检测,句号也不管. #include<bits/stdc++.h> using nam ...

  7. bzoj 1396: 识别子串 && bzoj 2865: 字符串识别【后缀数组+线段树】

    根据height数组的定义,和当前后缀串i最长的相同串的长度就是max(height[i],height[i+1]),这个后缀贡献的最短不同串长度就是len=max(height[i],height[ ...

  8. 9-26模拟赛 By cellur925

    1.计数 (count.cpp/c/pas)时间限制:1s内存限制:256MB[问题描述]给出 m 个数 a[1],a[2],…,a[m]求 1~n 中有多少数不是 a[1],a[2],…,a[m]的 ...

  9. Centos7.2下安装redis&通用键值命令

    Centos7.2下安装redis&通用键值命令 Centos7.2下安装redis 官方网站:https://redis.io/ 1.进入/usr/local/src/目录 cd /usr/ ...

  10. C++构造函数与析构函数的解析

    创建一个对象时,常常需要作某些初始化的工作,例如对数据成员赋初值. 注意,类的数据成员是不能在声明类时初始化的.如果一个类中所有的成员都是公用的,则可以在定义对象时对数据成员进行初始化.如: clas ...