zoukankan      html  css  js  c++  java
  • 【DFS或DP】Zipper

    总时间限制:

    1000ms
    内存限制:
    65536kB
    描述
    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".
    输入
    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.
    输出
    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.
    样例输入
    3
    cat tree tcraete
    cat tree catrtee
    cat tree cttaree
    
    样例输出
    Data set 1: yes
    Data set 2: yes
    Data set 3: no
    

    题目大意

    给你三个字符串,让你用前两个字符串组成第三个字符串,前两个字符串的长度和等于第三个字符串长度,前两个字符串可以随便拆,但是不能改变字符在字符串里的顺序。输入输出见样例。

    法一DFS(好理解一些)

    从头到尾,用a,b,c做下标遍历搜索三个字符串,如果s1[a]==s3[c],a++,c++如果s2[b]==s3[c],b++,c++。把遍历过的标记下来,避免大量的重复计算。

    参考代码

    #include <iostream>
    #include <string.h>
    using namespace std;
    char s1[201],s2[201],s3[402];
    int visited[201][201];//标记数组,下标分别表示第一个字符串和第二个字符串的长度
    int flag=0;//表示是否能组合
    int len3;//第三个字符串的长度
    void dfs(int a,int b,int c);
    int main()
    {
        int n,num=1;
        cin>>n;
        while(n){
            cin>>s1>>s2>>s3;
            len3=strlen(s3);
            memset(visited,0,sizeof(visited));
            dfs(0,0,0);
            if(flag==1){
                flag=0;
                cout<<"Data set "<<num<<": yes"<<endl;
            }
            else{
                cout<<"Data set "<<num<<": no"<<endl;
            }
            num++;
            n--;
        }
        return 0;
    }
    void dfs(int a,int b,int c){
        if(flag==1){//避免组合成功,函数返回的重复计算
            return;
        }
        if(c==len3){
            flag=1;
            return;
        }
        if(visited[a][b]==0){//避免组合不成功,函数返回的重复计算
            visited[a][b]=1;
            if(s1[a]==s3[c])
                dfs(a+1,b,c+1);
            if(s2[b]==s3[c])
                dfs(a,b+1,c+1);
        }
    }

    法二DP

    子问题:第一个字符串的前a个字母和第二个字符串前的b个字母能否组合成第三个字符串前a+b个字母。

    赋初值:第三个字符串的首字母或前几个字母一定是,第一个字符串的首字母或前几个字母,或第二个字符串的首字母或前几个字母。

    #include <iostream>
    #include <string.h>
    using namespace std;
    char s1[201],s2[201],s3[402];
    int dp[201][201];
    int a,b,c;//字符串的长度
    int main()
    {
        int n,num=1;
        cin>>n;
        while(n){
            cin>>(s1+1)>>(s2+1)>>(s3+1);
            a=strlen(s1+1);
            b=strlen(s2+1);
            c=strlen(s3+1);
        memset(dp,0,sizeof(dp));
        //初始化边界
        for(int i=1;i<=a;i++){
            if(s1[i]==s3[i]){
                dp[i][0]=1;
            }
            else{
                break;
            }
        }
        for(int i=1;i<=b;i++){
            if(s2[i]==s3[i]){
                dp[0][i]=1;
            }
            else{
                break;
            }
        }
    
        for(int i=1;i<=a;i++){//因为此处要看第三个字符串前(i+j)-1个字母是否能被组合所以必须从1开始遍历
            for(int j=1;j<=b;j++){
                if(s1[i]==s3[i+j]&&dp[i-1][j])
                    dp[i][j]=1;
                if(s2[j]==s3[i+j]&&dp[i][j-1])
                    dp[i][j]=1;
            }
        }
    
        if(dp[a][b]){
            cout<<"Data set "<<num<<": yes"<<endl;
        }
        else{
            cout<<"Data set "<<num<<": no"<<endl;
        }
    
            num++;
            n--;
        }
        return 0;
    }

    参考自:

    ①:http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD6/20151001/222588.html

    ②:http://www.cnblogs.com/yu-chao/archive/2012/02/26/2369052.html

  • 相关阅读:
    LeetCode 116. 填充每个节点的下一个右侧节点指针
    angluar 表单的验证 动态数据项表单验证
    Angular:ng-style,ng-class的使用
    1.splice(),slice(),split()快查
    js输入小写金额转大写
    Angular--CheckBox,checkbox多选,保存的时候用逗号隔开
    Angular--CheckBox
    Angular--Radio
    对于mysql中的group by分组后获取组内创建时间最大的那行数据
    GIT版本管理看这一篇就够了
  • 原文地址:https://www.cnblogs.com/LuRenJiang/p/7390657.html
Copyright © 2011-2022 走看看