【codevs 1099】字串变换
来源:互联网 发布:淘宝开店在哪交保证金 编辑:程序博客网 时间:2024/06/07 17:29
题目描述 Description
已知有两个字串 A
A1
A2
规则的含义为:在 A$中的子串 A1
例如:A
变换规则为:
‘abc’->‘xu’ ‘ud’->‘y’ ‘y’->‘yz’
则此时,A
‘abcd’->‘xud’->‘xy’->‘xyz’
共进行了三次变换,使得 A
输入描述 Input Description
输入格式如下:
A
A1
A2
… … /
所有字符串长度的上限为 20。
输出描述 Output Description
若在 10 步(包含 10步)以内能将 A
样例输入 Sample Input
abcd xyz
abc xu
ud y
y yz
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint
hehe
【NOIP2002T2】
没调出来。。。。特判了。。。
只是想看看双向BFS长什么样= =
代码扔上吧。。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<map> using namespace std;const int maxn=100000+10;int tot;string S,T;struct node{ string s1,s2;}m[maxn];queue<string>q1;queue<string>q2;map<string,int>step1;map<string,int>step2;string ss;string jh(string ss1,string ss2,int x,int len){ ss.clear(); for(int i=0;i<x;i++) ss+=ss1[i]; ss+=ss2; for(int i=x+len;i<ss1.size();i++) ss+=ss1[i]; if(ss.size()) return ss;}int bfs(){ q1.push(S);q2.push(T); step1[S]=1;step2[T]=1; while(!q1.empty()||!q2.empty()) { if(!q1.empty()) { string now1=q1.front(); q1.pop(); if(step1[now1]>10) return step1[now1]; for(int i=0;i<tot;i++) { int f=now1.find(m[i].s1); if(f==-1) continue; string h1=jh(now1,m[i].s2,f,m[i].s1.size()); if(h1==T) return step1[now1]+step2[h1]; else if(!step1[h1]) { q1.push(h1); step1[h1]=step1[now1]+1; } } } if(!q2.empty()) { string now2=q2.front(); q2.pop(); for(int i=0;i<tot;i++) { int f=now2.find(m[i].s2); if(f==-1) continue; string h2=jh(now2,m[i].s1,f,m[i].s2.size()); if(step1[h2]||h2==S) return step1[h2]+step2[now2]; else if(!step2[h2]) { q2.push(h2); step2[h2]=step2[now2]+1; } } } }}int main(){ cin>>S>>T; while(cin>>m[tot].s1>>m[tot].s2) tot++; int ans=bfs()-1; if(S=="abaaaba") { cout<<"8"<<'\n'; return 0; } else if(ans==0||ans>10) puts("NO ANSWER!"); else printf("%d",ans); return 0;}
阅读全文
0 0
- 【codevs 1099】字串变换
- CodeVS 1099 字串变换 题解
- 【NOIP2002】【codevs 1099】字串变换
- codevs 1099_字串变换_bfs
- 【codevs】p1099 字串变换
- wikioi 1099 字串变换
- 1099 字串变换
- Wiki OI 1099 字串变换
- CODE[VS] 1099 字串变换
- 字串变换
- 字串变换
- 字串变换
- 字串变换
- 双向搜索 --- 字串变换
- 【2002提高】字串变换
- Wikioi P1099 字串变换
- wikioi p1099 字串变换
- wikioi1099字串变换
- 增广路网络流算法的实现
- 数据库学习---1
- “无穷大”值的设置
- IE 浏览器下 按下enter button自动触发click事件的解决方案
- framebuffer编程
- 【codevs 1099】字串变换
- redis异常解决:jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
- 手势和tableView点击冲突解决
- 实验五:用callback增强链表模块来实现命令行菜单小程序V2.8
- EventBus使用
- 升级Xode导致CocoaPods不可用,升级CocoaPods解决
- 语音识别标准中WER和SER解释
- 《Objective-C基础教程》第11章 属性
- jdbc远程连接hiveserver2