CODE[VS] 1099字串变换(双向广搜)
来源:互联网 发布:淘宝 蒋凡 编辑:程序博客网 时间:2024/06/16 10:02
题目连接:
CODE[VS] 1099字串变换(双向广搜)
题目描述 Description
已知有两个字串 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 。
输入描述 Input Description
输入格式如下:
A
B A1B1 \ A2B2 |-> 变换规则 … … / 所有字符串长度的上限为
20。
输出描述 Output Description
若在 10 步(包含 10步)以内能将 A
变换为B ,则输出最少的变换步数;否则输出”NO ANSWER!”
样例输入 Sample Input
abcd xyz
abc xu
ud y
y yz
样例输出 Sample Output
3
分析:
双向BFS:已知起点终点的最短路问题
所谓双向搜索指的是搜索沿两个方向同时进行:
1.正向搜索:从初始结点向目标结点方向搜索。
2.逆向搜索:从目标结点向初始结点方向搜索。 当两个方向的搜索生成同一子结点时终止此搜索过程。双向搜索通常有两种方法:
1. 两个方向交替扩展。
2. 选择结点个数较少的那个方向先扩展。 方法2克服了两方向结点的生成速度不平衡的状态,明显提高了效率。
/************************************************************************* > File Name: 1099_字串变换.cpp > Author: dulun > Mail: dulun@xiyoulinux.org > Created Time: 2016年03月11日 星期五 13时19分48秒 ************************************************************************/#include<iostream>#include<stdio.h>#include<cstring>#include<string.h>#include<cstdlib>#include<algorithm>#define LL long longusing namespace std;const int N = 5086;struct Node{ char s[30];//过程中的字符串 char dep;//深度};Node list1[N], list2[N];char a[7][30] , b[7][30]; //两个数组,表示变换规则int n;//变换规则数bool IsSame(char *s1, char *s2) //判断两是否相同{ int t = strlen(s1); if(t != strlen(s2)) return false; for(int i = 0; i < t; i++) { if(s1[i] != s2[i]) return false; } return true;}bool IsIn(char * tmp, int cnt, int flag) //判断当前字符串从下标为start开始,是否与第cnt个变换规则相同{ char * cmp; // printf("DEbug: %s\n", tmp); if(flag == 1) cmp = a[cnt]; else cmp = b[cnt]; for(int i = 0; i < strlen(cmp);i++) { if(cmp[i] != tmp[i]) return false; } return true;}void bfs() //双向bfs,从起点向终点,从终点向起点{ int head1, tail1, head2, tail2, k; head1 = head2 = tail1 = tail2 = 1; while(head1 <= tail1 && head2 <= tail2) // { if(list1[head1].dep + list2[head2].dep > 10) {//步数大于10返回 printf("NO ANSWER!\n"); return ; } //从起点向终点向终点变换 for(int i = 0; i < strlen(list1[head1].s); i++) for(int j = 1; j <= n; j++) { if(IsIn(list1[head1].s+i, j, 1)) { tail1++; for(k = 0; k < i; k++)//写入没被规则替换的前半部分 { list1[tail1].s[k] = list1[head1].s[k]; } for(int l = 0; l < strlen(b[j]); l++, k++)//写入被规则替换过得部分 { list1[tail1].s[k] = b[j][l]; } for(int l = i+strlen(a[j]); l <= strlen(list1[head1].s) ; l++, k++)//写入没被替换的后半部分 { list1[tail1].s[k] = list1[head1].s[l]; } //善后工作 list1[tail1].s[k] = 0; list1[tail1].dep = list1[head1].dep+1; //判断首尾是否呼应 for(k = 1; k <= tail1; k++) { if(IsSame(list1[tail1].s, list2[k].s)) { printf("%d\n", list1[tail1].dep + list2[k].dep); return; } } } } for(int i = 0; i < strlen(list2[head2].s); i++) for(int j = 1; j <= n; j++) { if(IsIn(list2[head2].s+i, j, 2)) { tail2++; for(k = 0; k < i; k++)//写入没被规则替换的前半部分 { list2[tail2].s[k] = list2[head2].s[k]; } for(int l = 0; l < strlen(a[j]); l++, k++)//写入被规则替换过得部分 { list2[tail2].s[k] = a[j][l]; } for(int l = i+strlen(b[j]); l <= strlen(list2[head2].s) ; l++, k++)//写入没被替换的后半部分 { list2[tail2].s[k] = list2[head2].s[l]; } //善后工作 list2[tail2].s[k] = 0; list2[tail2].dep = list2[head2].dep+1; //判断首尾是否呼应 for(k = 1; k <= tail1; k++) { if(IsSame(list2[tail2].s, list1[k].s)) { printf("%d\n", list1[k].dep + list2[tail2].dep); return; } } } } head1++; head2++; } printf("NO ANSWER!\n");}int main(){ scanf("%s%s", list1[1].s, list2[1].s); n = 1; while(scanf("%s%s", a[n], b[n]) != EOF) n++;//最后一次+1要减掉 n--; //这里我一开始是这样写的,,wrong了无数回 //while(scanf("%s%s", a[n], b[n++]!=EOF); //n--; list1[1].dep = list2[1].dep = 0; bfs(); return 0;}//这题真是,,,,,,,,字符串处理都是自己写的,没用库函数,比较麻烦。
- CODE[VS] 1099字串变换(双向广搜)
- 天梯--字串变换(双向广搜)
- CODE[VS] 1099 字串变换
- 【NOIP2002】字串变换 CODE[VS] 1039
- Wiki OI 1099 字串变换(双向BFS)
- noip2002 字串变换 (双向宽搜,交替扩展)
- NOIP 2002字串变换 解题报告(双向宽搜)
- 双向搜索 --- 字串变换
- wikioi1099 字串变换 双向BFS
- voj1124 字串变换 双向bfs
- [codevs1099]字串变换 双向BFS
- 双向BFS—>NOIP2002 字串变换
- 洛谷 1032 [NOIP2002] 字串变换 双向bfs
- HDU 1401 Solitaire (双向广搜)
- POJ 1915(双向广搜)
- 双向广搜模版(360百科)
- hdu3085Nightmare Ⅱ(双向广搜)
- 双向广搜
- 用文字替换小图标(TextView替换ImageView)
- Oracle函数to_char应用示例
- 【Maven-Eclipse】Cannot change version of project facet Dynamic Web Module to 2.5.
- 文章标题
- 使用pl/sql备份数据
- CODE[VS] 1099字串变换(双向广搜)
- Java IO——UDP连接测试
- APNS推送服务证书制作 图文详解教程
- Powerdesigner反向工程从Mysql数据库导出数据结构的ODBC配置
- Kubernets
- TexturePacker打包后的文件在cocos2d-x加载和使用
- ACM内部函数--数学问题--大数相加
- Android微信支付爬坑
- 8.1.1 Android中的13种Drawable小结 Part 1