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): 10846    Accepted Submission(s): 3914


    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
     
    //把最后一个字符一个个销毁,之后比在销毁
    // cat tree tcraete 变为 ca tree tcraet 或cat tre tcraet
    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    char s1[205],s2[205],s3[410];
    int l1,l2,l3,p;
    bool f=0;
    void dfs(int x,int y,int z)
    {
        int i,j;
        if(x==0)
        {
            if(y!=z) return;
            for(i=0;i<y;i++)
            {
                    if(s2[i]!=s3[i]) return;
            }
            f=1;
        }
        if(y==0)
        {
            if(x!=z) return;
            for(i=0;i<x;i++)
            {
                    if(s1[i]!=s3[i]) return;
            }
            f=1;
        }
        if(s1[x-1]==s3[z-1])
        {
            dfs(x-1,y,z-1);
            if(f) return;
        }
        if(s2[y-1]==s3[z-1])
        {
            dfs(x,y-1,z-1);
            if(f) return;
        }
        return;
    }
    
    int main()
    {
        int t;
        cin>>t;
        for(p=1;p<=t;p++)
        {
            cin>>s1>>s2>>s3;
            f=0;
            l1=strlen(s1);
            l2=strlen(s2);
            l3=strlen(s3);
            if(l1+l2!=l3) 
            {
                cout<<"Data set "<<p<<": no"<<endl;continue;
            }
            if(s3[l3-1]!=s1[l1-1]&&s3[l3-1]!=s2[l2-1])
            {//剪枝1
                cout<<"Data set "<<p<<": no"<<endl;continue;
            }
            dfs(l1,l2,l3);
            if(f) printf("Data set %d: yes
    ",p);
            else   printf("Data set %d: no
    ",p);
        }
        return 0;
    }
  • 相关阅读:
    TCP断开连接的相关问题
    TCP建立连接的相关问题
    TCP的基本认识
    输入网址到网页显示,发生了什么
    leetcode_32.最长有效括号
    第六章:HTTP首部
    第五章:与HTTP协作的Web服务器
    RDF、RDFS、OWL
    分布式事务
    第四章:返回结果的HTTP状态码
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271278.html
Copyright © 2011-2022 走看看