题目大意:
问两个词能不能加错拼成一个第三个词。
题目分析:
dp方程还是很好想:dp[i][j]表示第一个词前i个和第二个词前j个能不能拼成第三个词的前i+j个。
初始化如果s1[1] == s[1] 那么dp[1][0] = true,s2[1] == s[2]那么dp[0][1] = true,
转移如下:$$dp[i][j] = dp[i - 1][j] (s1[i] == s[i + j) 或者 dp[i][j - 1] (s2[j] == s[i + j]) $$
两者只要一种满足则为true。
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
using namespace std;
const int N = 205;
char s[N], t[N], st[N * 2];
int lens, lent, T, k;
bool f[N][N];
int main(){
scanf("%d", &T);
while(T--){
memset(f, 0, sizeof f);
scanf("%s%s%s", s + 1, t + 1, st + 1); lens = strlen(s + 1), lent = strlen(t + 1);
if(s[1] == st[1]) f[1][0] = true;
if(t[1] == st[1]) f[0][1] = true;
for(int i = 0; i <= lens; i++)
for(int j = 0; j <= lent; j++){
int cnt = 0;
if(s[i] == st[i + j] && f[i - 1][j]) cnt++;
if(t[j] == st[i + j] && f[i][j - 1]) cnt++;
if(cnt) f[i][j] = true;
}
printf("Data set %d: %s
", ++k, (f[lens][lent] ? "yes" : "no"));
}
}