zoukankan      html  css  js  c++  java
  • 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;
    }





  • 相关阅读:
    《算法竞赛入门经典》《算法竞赛入门经典——训练之南》:勘误、讨论及代码
    codeforces 340B Maximal Area Quadrilateral(叉积)
    codeforces 340C Tourist Problem(简单数学题)
    codeforces 340A The Wall(简单数学题)
    UVALive 4043 Ants(二分图完美匹配)
    UVA 11865 Stream My Contest(最小树形图)
    UVA 11354 Bond(最小瓶颈路+倍增)
    UVALive 5713 Qin Shi Huang's National Road System(次小生成树)
    UVALive 3661 Animal Run(最短路解最小割)
    卡尔曼滤波器
  • 原文地址:https://www.cnblogs.com/Accepting/p/11271022.html
Copyright © 2011-2022 走看看