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 }

  • 相关阅读:
    java拦截器与过滤器打印请求url与参数
    mybatis学习笔记(六)使用generator生成mybatis基础配置代码和目录结构
    【IDEA】IDEA创建Maven的Web项目并运行以及打包
    【环境变量】Linux 下三种方式设置环境变量与获取环境变量
    【Git】GitHub的SSH提交配置[
    spring配置redis注解缓存
    【查看linux配置】查看linux系统常用的命令,Linux查看系统配置常用命令
    Redis集群
    linux中wget 、apt-get、yum rpm区别
    spring+redis的集成,redis做缓存
  • 原文地址:https://www.cnblogs.com/Annetree/p/5655317.html
Copyright © 2011-2022 走看看