题目描述 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$
   A1$ B1$ \
   A2$ B2$  |-> 变换规则
   ... ... / 
  所有字符串长度的上限为 20。

输出描述 Output Description

若在 10 步(包含 10步)以内能将 A$ 变换为 B$ ,则输出最少的变换步数;否则输出"NO ANSWER!"

样例输入 Sample Input

abcd xyz
abc xu
ud y
y yz

样例输出 Sample Output

3

数据范围及提示 Data Size & Hint

hehe

思路:
双向广搜
代码:
  1. #include<stdio.h>
  2. #include<string.h>
  3. struct node
  4. {
  5. char s[];
  6. int dep; //变换次数
  7. } list1[], list2[];
  8. char a[][], b[][];
  9. int n;
  10. void BFS()
  11. {
  12. int head1, tail1, head2, tail2, k;
  13. head1 = tail1 = head2 = tail2 = ;
  14. while(head1 <= tail1 && head2 <= tail2)
  15. {
  16. if(list1[head1].dep + list2[head2].dep > )
  17. {
  18. printf("NO ANSWER!\n");
  19. return ;
  20. }
  21. for(int i = ;i < strlen(list1[head1].s); i++)
  22. for(int j = ; j <= n; j++)
  23. if(strncmp(list1[head1].s + i, a[j], strlen(a[j])) == ) //寻找当前可变换的规则
  24. {
  25. tail1++; //移动尾指针,存储变换后的字符串,以下三个for循环为变换过程
  26. for(k = ; k < i; k++)
  27. list1[tail1].s[k] = list1[head1].s[k];
  28. for(int l = ; l < strlen(b[j]); l++, k++)
  29. list1[tail1].s[k] = b[j][l];
  30. for(int l = i + strlen(a[j]); l <= strlen(list1[head1].s); l++, k++)
  31. list1[tail1].s[k] = list1[head1].s[l];
  32. list1[tail1].s[k] = '\0'; //为变换结束后的字符串加结束符
  33. list1[tail1].dep = list1[head1].dep+;
  34. for (k = ; k <= tail1; k++)
  35. if (strcmp(list1[tail1].s, list2[k].s) == )//判断当前状态是否与逆向搜索交汇
  36. {
  37. printf("%d\n", list1[tail1].dep + list2[k].dep);
  38. return ;
  39. }
  40. }
  41. for (int i = ; i < strlen(list2[head2].s); i++) //逆向搜索同上
  42. for (int j = ; j <= n; j++)
  43. if(strncmp(list2[head2].s + i, b[j], strlen(b[j])) == )
  44. {
  45. tail2++;
  46. for(k = ; k < i; k++)
  47. list2[tail2].s[k] = list2[head2].s[k];
  48. for(int l = ; l < strlen(a[j]); l++, k++)
  49. list2[tail2].s[k] = a[j][l];
  50. for(int l = i + strlen(b[j]); l <= strlen(list2[head2].s); l++, k++)
  51. list2[tail2].s[k] = list2[head2].s[l];
  52. list2[tail2].s[k] = '\0';
  53. list2[tail2].dep = list2[head2].dep + ;
  54. for (k = ;k <= tail1; k++)
  55. if (strcmp(list1[k].s, list2[tail2].s) == )
  56. {
  57. printf("%d\n",list1[k].dep + list2[tail2].dep);
  58. return ;
  59. }
  60. }
  61. head1++;
  62. head2++;
  63. }
  64. printf("NO ANSWER!\n");
  65. }
  66. int main()
  67. {
  68. scanf("%s%s",list1[].s, list2[].s);
  69. n = ;
  70. while (scanf("%s%s",a[n],b[n]) != EOF)
  71. n++;
  72. n--;
  73. list1[].dep = list2[].dep = ;
  74. BFS();
  75. return ;
  76. }

codevs1099 字串变换的更多相关文章

  1. codevs1099字串变换(Bfs)

    /* 最少步数问题 妥妥的Bfs 很显然队列里存的是串(可能存个数也可以 就像8数码那样) 然后每次队首元素弄出来 能换的都换一遍 最后每次换完的新串入队前先判断到头了没 最后说一句 String大法 ...

  2. 「NOIP2002」「Codevs1099」 字串变换(BFS

    1099 字串变换 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 已知有两个字串 $A$, ...

  3. NOIP2002字串变换[BFS]

    题目描述 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A$中的子串 A1$ 可以变换为 B1$.A2 ...

  4. 字串变换(codevs 1099)

    题目描述 Description 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A$中的子串 A1$ ...

  5. NOIP2002 字串变换

    题二 字串变换 (存盘名: NOIPG2) [问题描述]: 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为: ...

  6. 字串变换 (2002 年NOIP全国联赛提高组)

    一道看似非常水的题 大意 :将一个字串 经过几种变换规则变为给定的另一个子串 ,求最小操作数. code[vs] 传送门 洛谷传送门 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): ...

  7. NOIP 2002 提高组 字串变换

    题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...

  8. 【洛谷1032 】【CJOJ1711】【NOIP2002】字串变换

    ###题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换 ...

  9. [NOIP2002]字串变换 T2 双向BFS

    题目描述 已知有两个字串  A,B  及一组字串变换的规则(至多6个规则): A1−>B1 A2−>B2 规则的含义为:在  A$中的子串  A1可以变换为可以变换为B1.A2可以变换为可 ...

随机推荐

  1. $CF1153A\ Serval\ and\ Bus$

    看大佬的代码都好复杂(不愧是大佬\(orz\) 蒟蒻提供一种思路 因为求的是最近的车对吧\(qwq\) 所以我们可以用一个\(while\)循环所以没必要去用什么 \(for...\) 至于这是\(d ...

  2. 整数类型c++

    数据类型 定义标识符 占字节数 数值范围 数值范围 短整型 short [int] 2(16位) -32768-32767 -215-215-1 整型 [long] int 4(32位) -21474 ...

  3. IDEA 激活方式

    最新的IDEA激活方式 使用网上传统的那种输入网址的方式激活不了,使用http://idea.lanyus.com/这个网站提供的工具进行 1.进入hosts文件中:C:\Windows\System ...

  4. Android 性能优化(12)网络优化( 8)Monitoring the Battery Level and Charging State

    Monitoring the Battery Level and Charging State PreviousNext This lesson teaches you to Determine th ...

  5. 如何调试ajax 和php

    ###ex11_1_main.php <html><head><meta http-equiv="Content-Type" content=&quo ...

  6. Storm编程入门API系列之Storm的可靠性的ACK消息确认机制

    概念,见博客 Storm概念学习系列之storm的可靠性  什么业务场景需要storm可靠性的ACK确认机制? 答:想要保住数据不丢,或者保住数据总是被处理.即若没被处理的,得让我们知道. publi ...

  7. 由一维数组表示的N维数组实现(C++)

    工作中,经常需要表示多维数组(如二维矩阵),常见的做法是使用T **pArr: T **pArr = new T*[M];//创建二维数组[M][N] ;i<M;i++) { pArr[i] = ...

  8. 1682. [HAOI2014]贴海报

    1682. [HAOI2014]贴海报 ★★☆   输入文件:ha14d.in   输出文件:ha14d.out   简单对比 时间限制:1 s   内存限制:256 MB [题目描述] Byteto ...

  9. Unity笔记(3)自学第二天

    学习记录: 界面使用: 脚本使用: 脚本注意点:

  10. Python中深拷贝与浅拷贝的区别

    转自:http://blog.csdn.net/u014745194/article/details/70271868 定义: 在Python中对象的赋值其实就是对象的引用.当创建一个对象,把它赋值给 ...