Combine String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1735 Accepted Submission(s): 507
Problem Description
Given three strings
a
,
b
and
c
, your mission is to check whether
c
is the combine string of
a
and
b
.
A string c is said to be the combine string of a and b if and only if c can be broken into two subsequences, when you read them as a string, one equals to a , and the other equals to b .
For example, ``adebcf'' is a combine string of ``abc'' and ``def''.
A string c is said to be the combine string of a and b if and only if c can be broken into two subsequences, when you read them as a string, one equals to a , and the other equals to b .
For example, ``adebcf'' is a combine string of ``abc'' and ``def''.
Input
Input file contains several test cases (no more than 20). Process to the end of file.
Each test case contains three strings a , b and c (the length of each string is between 1 and 2000).
Each test case contains three strings a , b and c (the length of each string is between 1 and 2000).
Output
For each test case, print ``Yes'', if
c
is a combine string of
a
and
b
, otherwise print ``No''.
Sample Input
abc def adebcf abc def abecdf
Sample Output
Yes No
Source
Recommend
liuyidin
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
const int M=2e3+10;
int i,j,k,n,m;
char s1[M],s2[M];
char s[M];
int dp[M][M];
int main()
{
int T;
int cas=1;
while(~scanf("%s %s %s",s1+1,s2+1,s+1)){
ms(dp,0);
int l1=strlen(s1+1),l2=strlen(s2+1),l3=strlen(s+1);
if((l1+l2)!=l3){printf("No\n");continue;};
dp[0][0]=1;
for(int i=0;i<=l1;i++)
for(int j=0;j<=l2;j++){
if(!i&&!j)continue;
if(s[i+j]==s1[i]&&s[i+j]==s2[j])
dp[i][j]=dp[i-1][j]||dp[i][j-1];
else if(s[i+j]==s1[i]){
if(i==0)dp[i][j]=1;
else dp[i][j]=dp[i-1][j];
}
else if(s[i+j]==s2[j]){
if(j==0)dp[i][j]=1;
else dp[i][j]=dp[i][j-1];
}
}
if(dp[l1][l2])printf("Yes\n");
else printf("No\n");
}
return 0;
}