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
思路:一开始模拟查找没有通过
dp[i][j] 记录的是 a的前i个和b的前j个是否能由c的前i+j个组成
#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 2e3 +5;
string a, b, c;
int dp[MAX][MAX];
int main()
{
while (cin >> a >> b >> c) {
memset(dp, 0, sizeof(dp));
if (a.size() + b.size() != c.size()){
cout << "No" << endl;
continue;
}
dp[0][0] = 1;
for (int i = 0; i <= a.size(); i++){
for (int j = 0; j <= b.size(); j++){
if (a[i] == c[i + j]){
dp[i + 1][j] |= dp[i][j]; //| 0|1=1 0|0=0 1|1=1,一真则真
}
if (b[j] == c[i + j]){
dp[i][j + 1] |= dp[i][j];
}
}
}
// for (int i = 0; i <= a.size(); i++){
// for (int j = 0; j <= b.size(); j++){
// cout << dp[i][j] << endl;
// }
// }
if (dp[a.size()][b.size()]){
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}