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;
    }
  • 相关阅读:
    vim 命令替换重复命令
    Python环境安装
    MySQL 查看show profile
    XSS攻击与CSRF攻击与防御
    HTTPS的原理
    PHP curl的请求步骤
    【论文阅读】HRNet
    【学习笔记】gRPC-python
    【Linux学习笔记】Linux基础
    【Golang学习笔记】入门:结构体、方法与接口
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271278.html
Copyright © 2011-2022 走看看