zoukankan      html  css  js  c++  java
  • HDU 1501 Zipper(DFS)

    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
    

    Source

    Pacific Northwest 2004
    女生赛类似的题
     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 char a[205],b[205],c[405];
     5 int l1,l2,l3;
     6 bool flag;
     7 int vis[205][205];
     8 void dfs(int x,int y,int z)
     9 {
    10     if(flag)
    11         return;
    12     if(z==l3)
    13     {
    14         flag=true;
    15         return;
    16     }
    17     if(vis[x][y]==1)
    18         return;
    19     vis[x][y]=1;
    20     if(a[x]==c[z])
    21         dfs(x+1,y,z+1);
    22     if(b[y]==c[z])
    23         dfs(x,y+1,z+1);
    24 }
    25 int main()
    26 {
    27     int T,t;
    28     cin>>T;
    29     for(t=1;t<=T;t++)
    30     {
    31         cin>>a>>b>>c;
    32         l1=strlen(a);
    33         l2=strlen(b);
    34         l3=strlen(c);
    35         flag=false;
    36         memset(vis,0,sizeof(vis));//防止超时
    37         if(l1+l2==l3)
    38         dfs(0,0,0);
    39         printf("Data set %d: ",t);
    40         if(flag)printf("yes
    ");
    41         else printf("no
    ");
    42     }
    43     return 0;
    44 }

  • 相关阅读:
    LDAP个人理解
    webpack-dev-middleware 与 webpack-hot-middlware
    RFC、EMCA-262、TC-39等名词
    贝塞尔曲线
    Async/await语法糖实现(Generator)
    Promise嵌套问题/async await执行顺序
    JS对象中,在原型链上找到属性后 最终将值拷贝给原对象 而不是引用
    三列布局中 float引发的一个问题-当“非float的元素”和“float的元素”在一起的时候,如果非float元素在先,那么float的元素将受到排斥。
    05-Linux系统编程-第02天(文件系统、目录操作、dup2)
    降低30%视频码率,窄带高清技术实现揭秘
  • 原文地址:https://www.cnblogs.com/Annetree/p/5655317.html
Copyright © 2011-2022 走看看