zoukankan      html  css  js  c++  java
  • 广搜——变换类

    Wikioi 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$
       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[30];
     6     int dep;  //变换次数 
     7 } list1[5010], list2[5010];
     8 char a[7][30], b[7][30];
     9 int n;
    10 void BFS()
    11 {
    12     int head1, tail1, head2, tail2, k;
    13     head1 = tail1 = head2 = tail2 = 1;
    14     while(head1 <= tail1 && head2 <= tail2)
    15     {
    16         if(list1[head1].dep + list2[head2].dep > 10) 
    17         {
    18             printf("NO ANSWER!
    ");
    19             return ;
    20         }
    21         for(int i = 0;i < strlen(list1[head1].s); i++)
    22             for(int j = 1; j <= n; j++)
    23                 if(strncmp(list1[head1].s + i, a[j], strlen(a[j])) == 0) //寻找当前可变换的规则 
    24                 {
    25                   tail1++; //移动尾指针,存储变换后的字符串,以下三个for循环为变换过程 
    26                   for(k = 0; k < i; k++) 
    27                       list1[tail1].s[k] = list1[head1].s[k];
    28                   for(int l = 0; 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] = ''; //为变换结束后的字符串加结束符 
    33                   list1[tail1].dep = list1[head1].dep+1;
    34                   for (k = 1; k <= tail1; k++)
    35                     if (strcmp(list1[tail1].s, list2[k].s) == 0)//判断当前状态是否与逆向搜索交汇 
    36                     {
    37                        printf("%d
    ", list1[tail1].dep + list2[k].dep);
    38                        return ;
    39                      }
    40                 }
    41         for (int i = 0; i < strlen(list2[head2].s); i++) //逆向搜索同上 
    42             for (int j = 1; j <= n; j++)
    43                 if(strncmp(list2[head2].s + i, b[j], strlen(b[j])) == 0)
    44                 {
    45                   tail2++;
    46                   for(k = 0; k < i; k++) 
    47                       list2[tail2].s[k] = list2[head2].s[k];
    48                   for(int l = 0; 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] = '';
    53                   list2[tail2].dep = list2[head2].dep + 1;
    54                   for (k = 1;k <= tail1; k++)
    55                     if (strcmp(list1[k].s, list2[tail2].s) == 0)
    56                     {
    57                        printf("%d
    ",list1[k].dep + list2[tail2].dep);
    58                        return ;
    59                     }
    60                 }
    61         head1++; 
    62         head2++;
    63     }
    64     printf("NO ANSWER!
    ");
    65 }
    66 int main()
    67 {
    68     scanf("%s%s",list1[1].s, list2[1].s);
    69     n = 1;
    70     while (scanf("%s%s",a[n],b[n]) != EOF) 
    71         n++;
    72 n--;
    73     list1[1].dep = list2[1].dep = 0;
    74     BFS();
    75     return 0;
    76 } 
    View Code
     
  • 相关阅读:
    JS站点
    1011 World Cup Betting (20分)
    1007 Maximum Subsequence Sum (25分)(动态规划DP)
    1006 Sign In and Sign Out (25分)
    1005 Spell It Right (20分)
    1004 Counting Leaves (30分)(DFS)
    1003 Emergency (25分)(Dijkstra算法)
    1002 A+B for Polynomials (25分)
    1001 A+B Format (20分)
    canvas
  • 原文地址:https://www.cnblogs.com/hyfer/p/4841860.html
Copyright © 2011-2022 走看看