J--Zipper

J--Zipper

时间: 1ms        内存:128M

描述:

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

提示:

参考答案(内存最优[964]):

#include <stdio.h>
#include <string.h>
int main()
{
    char str1[201],str2[201],str3[401];
    int n,sum=1;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s%s%s",str1,str2,str3);
        int len1=strlen(str1),len2=strlen(str2);
        int i,j=0,k=0;
        for(i=0; i<len1+len2; ++i)
        {
            if(j<len1 && str1[j]==str3[i])
                ++j;
            if(k<len2 && str2[k]==str3[i])
                ++k;
        }
        if(j==len1 && k==len2)
            printf("Data set %d: yes\n",sum++);
        else printf("Data set %d: no\n",sum++);
    }
    return 0;
}

参考答案(时间最优[0]):

#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        char a[250];
        char b[250];
        char c[550];
        scanf("%s%s%s",a,b,c);
        int la=strlen(a);
        int lb=strlen(b);
        int lc=strlen(c);
        int dp[250][250];
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        if(a[0]==c[0])
        dp[1][0]=1;
        if(b[0]==c[0])
            dp[0][1]=1;
        for(int j=0;j<=la;j++)
        {         
            for(int k=0;k<=lb;k++)
            {
                if(k>=1&&c[k+j-1]==b[k-1])
                    dp[j][k]=dp[j][k]||dp[j][k-1];
                if(j>=1&&c[k+j-1]==a[j-1])
                    dp[j][k]=dp[j][k]||dp[j-1][k];
            }     
        }     
        if(dp[la][lb])
            cout<<"Data set "<<i<<": yes"<<endl;    
        else      
            cout<<"Data set "<<i<<": no"<<endl;
	}   
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注