zoukankan      html  css  js  c++  java
  • hdu 1501 Zipper(dfs)

    hdu 1501 Zipper

    Problem Description
    Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.

    For example, consider forming "tcraete" from "cat" and "tree":

    String A: cat
    String B: tree
    String C: tcraete


    As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree":

    String A: cat
    String B: tree
    String C: catrtee


    Finally, notice that it is impossible to form "cttaree" from "cat" and "tree".
     

    Input
    The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.

    For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.

     

    Output
    For each data set, print:

    Data set n: yes

    if the third string can be formed from the first two, or

    Data set n: no

    if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.
     

    Sample Input
    3 cat tree tcraete cat tree catrtee cat tree cttaree
     

    Sample Output
    Data set 1: yes Data set 2: yes Data set 3: no

    代码:

    #include<iostream>
    using namespace std;
    char sa[210],sb[210],sc[420];
    int la,lb,lc,flag;
    int mark[210][210];
    void dfs(int x,int y,int z)
    {
     if(flag)
      return;
     if(z>=lc)
     {
      flag=1;
      return;
     }
     if(mark[x][y])
      return;
     mark[x][y]=1;
     if(sa[x]==sc[z]&&x<la)
      dfs(x+1,y,z+1);
     if(sb[y]==sc[z]&&y<lb)
      dfs(x,y+1,z+1);
    }
    int main()
    {
     int t,k;
     while(~scanf("%d",&t))
     {
      for(k=1;k<=t;k++)
      {
       scanf("%s%s%s",sa,sb,sc);
       la=strlen(sa);
       lb=strlen(sb);
       lc=strlen(sc);  
       if(lc!=la+lb)
       {
        printf("Data set %d: no\n",k);
        continue;
       }
       flag=0;
       memset(mark,0,sizeof(mark));
       dfs(0,0,0);
       if(flag)
        printf("Data set %d: yes\n",k);
       else
        printf("Data set %d: no\n",k);
      }
     }
     return 0;
    }

  • 相关阅读:
    转:测试驱动开发全攻略
    转:如何提高自己的归纳总结能力?
    转:从编译链接过程解析static函数的用法
    C++ 不能在类体外指定关键字static
    转:画图解释 SQL join 语句
    转:[置顶] 从头到尾彻底理解KMP(2014年8月22日版)
    转:The Knuth-Morris-Pratt Algorithm in my own words
    转:数组与指针的区别
    删除单链表中间节点
    如果判断两个单链表有交?第一个交点在哪里?
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2999493.html
Copyright © 2011-2022 走看看