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# 二进制读写
    重构风险程序员一定要遵守的规则
    文件区域使用fcntl锁定文件,并且测试
    数据清空js清空div里的数据问题
    模板缓存ThinkPHP中的模板引擎和视图层
    描述null11121 Base 2
    操作系统请求操作系统 算法
    工程项目eclipse项目名前出现红色感叹号,小红叉解决
    程序链接关于静态链接,动态链接,共享库,ABI的一些记录(os学习)
    NMAKE命令行编译
  • 原文地址:https://www.cnblogs.com/Annetree/p/5655317.html
Copyright © 2011-2022 走看看