zoukankan      html  css  js  c++  java
  • HDU 1501 Zipper

    Zipper

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4575    Accepted Submission(s): 1629

    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
     
    Recommend
    linle
     
     
     
    #include<stdio.h>
    #include<string.h>
    
    const int maxn=220;
    
    char str1[maxn],str2[maxn],str3[2*maxn];
    int len1,len2,len3;
    int visited[maxn][maxn],flag;
    
    void DFS(int a,int b,int c){
        if(flag)
            return ;
        if(c==len3){
            flag=1;
            return ;
        }
        if(!visited[a][b]){
            visited[a][b]=1;
            if(str1[a]==str3[c])
                DFS(a+1,b,c+1);
            if(str2[b]==str3[c])
                DFS(a,b+1,c+1);
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n,cases=0;
        scanf("%d",&n);
        while(n--){
            scanf("%s%s%s",str1,str2,str3);
            len1=strlen(str1);
            len2=strlen(str2);
            len3=strlen(str3);
            printf("Data set %d: ",++cases);
            if(len1+len2!=len3){
                printf("no\n");
                continue;
            }
            memset(visited,0,sizeof(visited));
            flag=0;
            DFS(0,0,0);
            if(flag)
                printf("yes\n");
            else
                printf("no\n");
        }
        return 0;
    }
  • 相关阅读:
    【剑指offer】面试题40:数组中只出现一次的数字
    【剑指offer】面试题39扩展:平衡二叉树
    【剑指offer】面试题39:二叉树的深度
    【剑指offer】面试题38:数字在排序数组中出现的次数
    【剑指offer】面试题37:两个链表的第一个公共结点
    【剑指offer】面试题36:数组中的逆序对
    剑指Offer
    设计模式
    ACM
    算法设计与分析
  • 原文地址:https://www.cnblogs.com/jackge/p/2984386.html
Copyright © 2011-2022 走看看