poj2192 Zipper 两个字符串是否能构成第三个

来源:互联网 发布:阿里云os 系统精简 编辑:程序博客网 时间:2024/05/17 05:56
Zipper
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13390 Accepted: 4723

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

3cat tree tcraetecat tree catrteecat tree cttaree

Sample Output

Data set 1: yesData set 2: yesData set 3: no

Source

Pacific Northwest 2004
//dp[i][j]表示s1串的前i个字符和s2的前j个字符能否组成str的前i+j个字符//dp[i][j]只能由dp[i-1][j]和dp[i][j-1]转移过来#include<iostream>#include<cstdlib>#include<stdio.h>#include<memory.h>#include<string.h>using namespace std;char s1[205];char s2[205];char str[410];int dp[205][205];int main(){    int t;    int count=1;    scanf("%d",&t);    while(t--)    {        scanf("%s%s%s",s1,s2,str);        memset(dp,0,sizeof(dp));        int l1=strlen(s1);        int l2=strlen(s2);        for(int i=1;i<=l1;i++)        if(s1[i-1]==str[i-1])        dp[i][0]=1;        for(int j=1;j<=l2;j++)        if(s2[j-1]==str[j-1])        dp[0][j]=1;        for(int i=1;i<=l1;i++)        for(int j=1;j<=l2;j++)        {            if(s1[i-1]==str[i+j-1]&&dp[i-1][j])            dp[i][j]=1;            if(s2[j-1]==str[i+j-1]&&dp[i][j-1])            dp[i][j]=1;        }        printf("Data set %d: ",count++);        if(dp[l1][l2]==1)        puts("yes");        else        puts("no");    }}

#include<iostream>#include<cstdio>#include<string.h>using namespace std;const int N=205;int dp[N][N];char s1[N],s2[N];char str[2*N];int main(){    int t;    scanf("%d",&t);    int count=1;    while(t--)    {        scanf("%s%s%s",s1+1,s2+1,str+1);        int l1=strlen(s1+1);        int l2=strlen(s2+1);        memset(dp,-1,sizeof(dp));        dp[0][0]=dp[0][1]=dp[1][0]=1;        for(int i=0;i<=l1;i++)        for(int j=0;j<=l2;j++)        {            if(i!=0&&s1[i]==str[i+j]&&dp[i-1][j]==1)            dp[i][j]=1;            if(j!=0&&s2[j]==str[i+j]&&dp[i][j-1]==1)            dp[i][j]=1;        }        printf("Data set %d: ",count++);        if(dp[l1][l2]==1)        puts("yes");        else        puts("no");    }}

原创粉丝点击