Given three strings aa, bb and cc, your mission is to check whether cc is the combine string of aa and bb.
A string cc is said to be the combine string of aa and bb if and only if cc can be broken into two subsequences, when you read them as a string, one equals to aa, and the other equals to bb.
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 aa, bb and cc (the length of each string is between 1 and 2000).
Output
For each test case, print ``Yes'', if cc is a combine string of aa and bb, otherwise print ``No''.
Sample Input
abc def adebcf abc def abecdf
Sample Output
Yes No
两个字符串是否能组成另一个字符串,不要求连续,但字符顺序不变
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=4005;
int dp[maxn][maxn];
int main()
{
string str1,str2,str0;
while(cin>>str1>>str2>>str0){
memset(dp,0,sizeof(dp));//dp[i][j]=0代表str1的 i个字符和 str2 的j个字符不能组成str0的i+j个字符
int l0=str0.size(),l1=str1.size(),l2=str2.size();
dp[0][0]=1;
if(l0==l1+l2)
{
dp[0][0]=1;
for(int i=0; i<=l1; i++)
{
for(int j=0; j<=l2; j++)
{
if(str1[i]==str0[i+j]&&dp[i][j]==1) //str1的第i个字符 === str0的前i+j个字符
{
dp[i+1][j]=dp[i][j]; //str1的前i+1个字符加上 str2的前j个字符等于str0的i+j个字符
}
if(str2[j]==str0[i+j]&&dp[i][j]==1)//dp[i][j+1]的状态与dp[i][j]的状态一样
// 也可以写作dp[i][j+1] | = dp[i][j]
{
dp[i][j+1]=dp[i][j];
}
}
}
if(dp[l1][l2]==1)
{
cout<<"Yes"<<endl;
}
else
cout<<"No"<<endl;
}
else {
cout<<"No"<<endl;
}
}
return 0;
}