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''.
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).
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
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).
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
思路:感觉很简单想爆搜,害怕超时,指针也不会搞,参考了题解。。没有往dp方向上想,发现dp真的好厉害。
就是用dp【i】【j】来表示A串前i位和B串前j位是否已经匹配。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int dp[2010][2010];
int main()
{
int num1,num2,num3;
string a,b,c;
while(cin>>a>>b>>c)
{
num1=a.length();
num2=b.length();
num3=c.length();
memset(dp,0,sizeof(dp));
if(num1+num2!=num3)
{
cout<<"No"<<endl;
}
else
{
dp[0][0]=1;
for(int i=0;i<=num1;i++)
{
for(int j=0;j<=num2;j++)
{
if(a[i]==c[i+j])
{
dp[i+1][j]|=dp[i][j];
}
if(b[j]==c[i+j])
{
dp[i][j+1]|=dp[i][j];
}
}
}
/*for(int i=0;i<=num1;i++)
{
for(int j=0;j<=num2;j++)
{
cout<<"i: "<<i<<" j:"<<j<<" "<<dp[i][j]<<"$$$$$"<<endl;
}
}
*/
if(dp[num1][num2]==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
return 0;
}