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 }

  • 相关阅读:
    C#中处理鼠标和键盘的事件
    C#中处理鼠标和键盘的事件
    C#中处理鼠标和键盘的事件
    mpich2安装
    算法题推箱子
    LINUX终端下windows盘的位置
    Linux头文件和库文件添加环境变量与GCC编译器添加INCLUDE与LIB环境变量
    第九章顺序容器重学C++之《 C++ PRIMER》
    sed中使用变量
    抛出异常
  • 原文地址:https://www.cnblogs.com/Annetree/p/5655317.html
Copyright © 2011-2022 走看看