Zipper
Time Limit: 1000MS Memory Limit: 65536K
Total 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
3
cat tree tcraete
cat tree catrtee
cat tree cttaree
Sample Output
Data set 1: yes
Data set 2: yes
Data set 3: no
Source
Pacific Northwest 2004
[cpp]
//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");
}
}