传送门
题解:
如果定义状态dp[0/1][i]表示将c[i]分给a/b串,那么遇到样例2那种情况很可能走进死路(把’t’分给a串)然后无法撤销,所以定义状态dp[i][j]表示a串前i个字母与b串前j个字母能否组成c串前(i+j)个字母。由于要求有序,所以i从i-1转移,j从j-1转移。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=202;
char a[MAXN],b[MAXN],c[MAXN<<1];
bool dp[MAXN][MAXN];
int main() {
// freopen("hdu 1501.in","r",stdin);
int kase;
scanf("%d",&kase);
for (int t=1;t<=kase;++t) {
memset(dp,false,sizeof(dp));
scanf("%s%s%s",a+1,b+1,c+1);
int l1=strlen(a+1),l2=strlen(b+1);
for (int i=1;i<=l1;++i)
dp[i][0]=(a[i]==c[i]);
for (int j=1;j<=l2;++j)
dp[0][j]=(b[j]==c[j]);
for (int i=1;i<=l1;++i)
for (int j=1;j<=l2;++j)
dp[i][j]=(a[i]==c[i+j]&&dp[i-1][j])||(b[j]==c[i+j]&&dp[i][j-1]);
printf("Data set %d: %s\n",t,dp[l1][l2]?"yes":"no");
}
return 0;
}