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 }

  • 相关阅读:
    TouTiao开源项目 分析笔记19 问答内容
    TouTiao开源项目 分析笔记18 视频详情页面
    TouTiao开源项目 分析笔记17 新闻媒体专栏
    TouTiao开源项目 分析笔记16 新闻评论
    TouTiao开源项目 分析笔记15 新闻详情之两种类型的实现
    TouTiao开源项目 分析笔记14 段子评论
    计算机专业大学课程学习路线图
    Windows GitLab使用全过程
    2017年最后一天
    生成6个1~33之间的随机整数,添加到集合,并遍历集合
  • 原文地址:https://www.cnblogs.com/Annetree/p/5655317.html
Copyright © 2011-2022 走看看