字串变换
来源:互联网 发布:unity3d litjson 编辑:程序博客网 时间:2024/06/06 20:45
字串变换
来源:
2002年NOIP全国联赛提高组
算法使用:
BFS+STL字符串处理
题目描述:
已知有两个字串 A,B 及一组字串变换的规则(至多6个规则):
A1−>B1
A2−>B2
规则的含义为:在 A$中的子串 A1可以变换为B1 、A2可以变换为B2 …。
例如:A=′abcd′B =’xyz’ 变换规则为:
‘abc’->‘xu’ ‘ud’->‘y’ ‘y’->‘yz’
则此时,A可以经过一系列的变换变为B ,其变换的过程为: ‘abcd’->‘xud’->‘xy’->‘xyz’
共进行了三次变换,使得 A变换为B 。
输入描述:
输入格式如下:
A B
A2 B2
……
所有字符串长度的上限为 20。
输出描述:
若在 10 步(包含 10步)以内能将 A变换为B ,则输出最少的变换步数;否则输出”NO ANSWER!”
样例输入:
abcd xyz
abc xu
ud y
y yz
样例输出:
3
#include<iostream>#include<string>#include<queue>#include<map>using namespace std;struct node{ string s; int step;};map<string,int> visit;string sa,sb,s1[10001],s2[10001];queue<node> que;int n=1;void init(){ cin>>sa>>sb; while(cin>>s1[n]>>s2[n]) n++; n--;}void bfs(){ node e; e.step=0; e.s=sa; que.push(e); while(!que.empty()) { node t=que.front(); que.pop(); if(t.s==sb&&t.step<11) { cout<<t.step; return; } if(visit[t.s]==0) { visit[t.s]=1; for(int i=1;i<=n;i++) { if(t.s.find(s1[i])>=0) { for(int j=t.s.find(s1[i]);j>=0&&j<=t.s.size()-s2[i].size();j=t.s.find(s1[i],j+1)) { node w=t; w.step++; w.s.replace(j,s1[i].size(),s2[i]); que.push(w); } } } } } cout<<"NO ANSWER!";}int main(){ init(); bfs(); return 0;}
1 0
- 字串变换
- 字串变换
- 字串变换
- 字串变换
- 双向搜索 --- 字串变换
- 【2002提高】字串变换
- Wikioi P1099 字串变换
- wikioi p1099 字串变换
- wikioi1099字串变换
- 【codevs】p1099 字串变换
- 【NOIP 2002】字串变换
- 洛谷1032 字串变换
- 【NOIP2002】字串变换 -宽搜
- NOIP2002 字串变换
- 【洛谷 1032】 字串变换
- 洛谷 P1032 字串变换
- 洛谷1032 字串变换
- 洛谷 P1032 字串变换
- cifar代码
- Mysql与Sql server在语法和关键字上的区别
- 从什么都不懂开始(二)——创建Project提交到Github需要做什么
- Exception异常处理
- Android 事件分发机制(最新源码6.0分析)--childView
- 字串变换
- QT国际化翻译的使用
- Java笔记之socket中的SSL
- 找字符串中第一个只出现一次的字符
- Redhat Enterprise 7.2 安装 MySQL 5.7
- HDU 2897 Bash博弈变形
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- SaaS WMS与传统WMS的区别
- 如何让本机mysql可以远程连接