【搜索】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
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 10; 4 string A,B,a[N],b[N]; 5 map<string,int>Mp; 6 7 typedef struct Node{ 8 string S; 9 int step; 10 }Node; 11 12 int cnt = 0; 13 int Check(string u,string v,int pos = 0 ){ 14 int L1 = u.length() ; 15 int L2 = v.length() ; 16 int i = pos , j = 0 ; 17 while( i < L1 && j < L2 ){ 18 while( u[i] == v[j] ){ 19 i++; 20 j++; 21 if( j == L2 ){ 22 return i-L2; 23 } 24 } 25 j = 0; 26 i ++ ; 27 } 28 return -1; 29 } 30 int ans = -1 ; 31 void bfs( ){ 32 33 queue<Node>Q; 34 Q.push ( Node{A,0} ); 35 Mp[A] = 1; 36 while ( !Q.empty() ){ 37 Node cur = Q.front(); 38 Q.pop(); 39 if( cur.step >= 11 ) continue; 40 if( cur.S == B ){ 41 ans = cur.step; 42 return ; 43 } 44 for(int i=0;i<cnt;i++){ 45 string tmp = cur.S; 46 int Len = tmp.length(); 47 int L = a[i].length(); 48 for( int j=0 ; j < Len ; j++ ) { 49 int pos = Check(tmp,a[i],j); 50 if( pos != -1 && pos != 0 ){ 51 string s1 = tmp.substr(0,pos); 52 string s2 = b[i]; 53 string s3 = tmp.substr(pos+L); 54 string t = s1+s2+s3; 55 //cout<<t<<endl; 56 if( Mp[t] == 0 ){ 57 Mp[t] = 1; 58 Q.push(Node{t,cur.step+1}); 59 } 60 } 61 if( pos == 0 ){ 62 string s1 = b[i]; 63 string s2 = tmp.substr(pos+L); 64 string t = s1 + s2 ; 65 //cout<<t<<endl; 66 if( Mp[t] == 0 ){ 67 Mp[t] = 1; 68 Q.push(Node{t,cur.step+1}); 69 } 70 } 71 } 72 } 73 } 74 } 75 int main() { 76 77 ios_base :: sync_with_stdio(NULL); 78 cin.tie(NULL); 79 cout.tie(NULL); 80 81 cin>>A>>B; 82 83 while( cin>>a[cnt]>>b[cnt] ){ 84 85 if( a[cnt] == "1" && b[cnt]=="1") 86 break; 87 cnt++; 88 } 89 bfs(); 90 if(ans==-1){ 91 puts("NO ANSWER!"); 92 }else{ 93 printf("%d\n",ans); 94 } 95 return 0; 96 }
View Code
转载于:https://www.cnblogs.com/Osea/p/10890405.html
【搜索】P1032 字串变换相关推荐
- P1032 字串变换(bfs)
https://www.luogu.org/problemnew/show/P1032 题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A 1-> B 1 A 2 ...
- 洛谷P1032字串变换题解--zhengjun
题目描述 已知有两个字串A,BA,BA,B及一组字串变换的规则(至多666个规则): A1A_1A1 −-−> B1B_1B1 A2A_2A2 −-−> B2B_2B2 规则的含义 ...
- 洛谷P1032 字串变换
字符串的题目一般都很恶心,特别是当你妄图只用原生c语言的char去做的时候. string类虽然方便,但是相较于char慢很多.可是慢所带来的好处就是可以方便的完成很多操作.例如用string去实现这 ...
- P1032 字串变换
题面:https://www.luogu.org/problem/P1032 本题中需熟练掌握string类型的库函数: string a,b; a.find(b,pos)为 从pos开始找b第一次出 ...
- 【题解】luogu p1032 字串变换
总结: 1.bfs的模板没有掌握好 2.string s 的时候可以用s[m]=x 将s字符串中的m位置的字符改变成x字符 一道简单的bfs #include<bits/stdc++.h> ...
- 广度优先搜索——字串变换(洛谷 P1032)
题目选自洛谷P1032 题意已经把做法写得特别露骨了...最小步数,最多6个变换规则....广搜自不必说,不仅可以寻找解而且还能判断步数(根据广搜首解最优的性质可以得到). 用queue来进行广搜,需 ...
- 字串变换(洛谷-P1032)
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...
- P1032 [NOIP2002 提高组] 字串变换
展开 题目背景 本题疑似错题,不保证存在靠谱的多项式复杂度的做法.测试数据非常的水,各种做法都可以通过,不代表算法正确.因此本题题目和数据仅供参考. 题目描述 已知有两个字串 A,BA,B 及一组字串 ...
- [COGS 0065][NOIP 2002] 字串变换
65. [NOIP2002] 字串变换 ★★ 输入文件:string.in 输出文件:string.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 已知有两个 ...
- NOIP 2002 字串变换
题目描述 已知有两个字串A,B,B,B及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A中的子串A1中的子串A1中的子串A1可以变换为 ...
最新文章
- 用python画爱心动图_编写Python爬虫抓取暴走漫画上gif图片的实例分享
- Hadoop Hive导入数据命令
- JavaScript——易班优课YOOC课群在线测试自动答题解决方案(十六)利用PC端和移动端BUG
- 英特尔核芯显卡控制面板没有了_核显和独显、集成显卡有什么区别
- 不好意思,00后黑客CEO登场了!
- 阿里、腾讯隔空“对话”互联互通 打破垄断让中小商户受益是核心命题
- Windows里Anaconda-Navigator无法打开(解决)
- android studio for android learning (六)在布局中常使用的单位
- 02-02 逐帧动画、多组动画案例 实现小人跑步效果
- 端子排延时中间继电器DZS-822/DC110V
- python安装pytorch
- html中写一个占内存很大死循环代码,HTML5新特性Bug:这12行代码分分钟让你浏览器崩溃iPhone重启...
- php生成gif1009php生成gif,怎样将几张图片做成会动的GIF的动态图像?GIF动画制作软件,将图片制作成GIF动图...
- 基本排序算法 《数据结构》严蔚敏版
- 修改电脑软件默认安装位置、下载位置
- 互联网低成本的创业与变现
- qt入门,最简单的qt程序
- Unable to make field private final byte[] java.lang.String.value accessible: module java.base does n
- jsp的代码格式的基础知识
- idea启动卡死的问题