• Zipper 杭电 1501


    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". 

    InputThe 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. 

    OutputFor 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
    题目大意就是 在不改变字符串1和字符串2的顺序的情况下能否组成字符串3
    思路::用一个二维数组dp[x][y]来标记字符串1的位置X和字符串2的位置y是否被访问过
    #include<iostream>
    #include<string>
    #include<cstring>
    using namespace std;
    string a,b,c;
    int flag=0;
    int dp[201][201];
    void dfs(int x,int y,int z){
        if(flag) return;
        if(x==a.size()&&y==b.size()){
            flag=1;
            return ;
        }
        if(dp[x][y]) return ;
        dp[x][y]=1;
        if(a[x]==c[z]){
            
            dfs(x+1,y,z+1);
        }
        if(b[y]==c[z]){
            dfs(x,y+1,z+1);
        }
    }
    int main()
    {
        int t;
        cin>>t;
        for(int i=1;i<=t;i++){
            cin>>a>>b>>c;
            memset(dp,0,sizeof(dp));
            flag=0;
            dfs(0,0,0);
            printf("Data set %d: ",i); 
            if(flag)
                puts("yes");
            else puts("no");
            a="",b="",c="";
        }
        return 0;
    }





  • 相关阅读:
    孔曰成仁,孟曰取义
    mysql索引&实现原理
    MySQL存储引擎
    HashMap原理
    反射
    list对象属性排序
    mysql数据表操作&库操作
    mysql字段类型
    java线程池初步理解
    java四种内部类
  • 原文地址:https://www.cnblogs.com/Accepting/p/11271022.html
Copyright © 2020-2023  润新知