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;
}