• POJ 2192


    Zipper
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13041   Accepted: 4560

    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
    /*
    大致题意:输入三个字符串,判断前两个字符串能否组成最后一个,
    使得各字母在原单词中的相对顺序不变
    */ 
    #include<stdio.h>
    #include<string.h>
    char str1[210],str2[210],str[420];
    bool vis[210][210];
    bool dp(char *str1,char *str2,char *str)//原来把参数顺序传错啦
    {
        int i,j;
        int len1=strlen(str1);
        int len2=strlen(str2);
        int len=strlen(str);
        if(len1+len2!=len)
            return false;
        for(i=1;i<=len1;i++)
            vis[i][0]=false;
        for(i=1;i<=len2;i++)
           vis[0][i]=false;
        vis[0][0]=true;
        for(i=0;i<=len1;i++)
            for(j=0;j<=len2;j++)
            {
                if(i>0&&str1[i-1]==str[i+j-1]&&vis[i-1][j])
                    vis[i][j]=true;
                if(j>0&&str2[j-1]==str[i+j-1]&&vis[i][j-1])
                    vis[i][j]=true;
            }
        return vis[len1][len2];
    }
    int main()
    {
        int i,j,T;
        bool flag;
        scanf("%d%*c",&T);
        j=1;
        while(T--)
        {
            memset(str,0,sizeof(str));
            memset(str1,0,sizeof(str1));
            memset(str2,0,sizeof(str2));
            memset(vis,0,sizeof(vis));
            scanf("%s",str1);
           // puts(str1);
            scanf("%s",str2);
           // puts(str2);
            scanf("%s",str);
          //  puts(str);          
            flag=dp(str1,str2,str);
            /*
            for(i=0;i<15;i++)
            {  
                 for(j=0;j<15;j++)
                     printf("%d ",vis[i][j]);
                putchar('\n');
            }
            */
            printf("Data set %d: %s\n",j++,flag==true?"yes":"no");
        }
        return 0;
    }
    
  • 相关阅读:
    基于单片机定时器---年月日时分秒的算法
    按位查询算法---基于Perimiter Sensor
    MFC File crc 计算
    二叉树的层次遍历

    动态规划
    贪心算法 动态规划
    爱信息图床测试
    前端小白的个人习惯和笔记(一)
    过年了,是不是应该写点代码祝福别人
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2619731.html
Copyright © 2020-2023  润新知