状态dp[i][j]表示字符串1的前i个字符与字符串2的前j个字符是否能构成字符串3的前i+j个字符
#include <stdio.h>
#include <string.h>
#include<functional>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 215;
const int inf = 1<<30;
typedef __int64 LL;
int len1,len2;
bool dp[maxn][maxn];
char str1[maxn],str2[maxn],str[maxn<<1];
void GetDp()
{
memset( dp,0,sizeof(dp) );
dp[0][0] = 1;
for( int i = 0; i <= len1; i ++ ){
for( int j = 0; j <= len2; j ++ ){
if( i == j && i == 0 ) continue;
if( i != 0 && str1[i-1] == str[i+j-1] && dp[i-1][j] )
dp[i][j] = 1;
else if( j != 0 && str2[j-1] == str[i+j-1] && dp[i][j-1] )
dp[i][j] = 1;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cas;
scanf("%d",&cas);
for( int i = 1; i <= cas; i ++ )
{
scanf("%s%s%s",str1,str2,str);
len1 = strlen( str1 );
len2 = strlen( str2 );
printf("Data set %d: ",i);
GetDp();
if( dp[len1][len2] )
puts("yes");
else
puts("no");
}
return 0;
}
DFS
#include <stdio.h>
#include <string.h>
#include<functional>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 215;
const int inf = 1<<30;
typedef __int64 LL;
int len,len1,len2;
char str1[maxn],str2[maxn],str[maxn<<1];
int flag;
bool Hash[maxn][maxn];
bool DFS( int k,int i,int j )
{
if( str[k] == '\0' ) return 1;
if( Hash[i][j] ) return 0;
Hash[i][j] = 1;
if( str1[i] == str[k] && DFS( k+1,i+1,j) )return 1;
if( str2[j] == str[k] && DFS( k+1,i,j+1) ) return 1;
return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cas,c = 1;
scanf("%d",&cas);
while( cas -- )
{
flag = 0;
scanf("%s%s%s",str1,str2,str);
memset( Hash,0,sizeof(Hash) );
printf("Data set %d: ",c++);
if( DFS(0,0,0) )
puts("yes");
else
puts("no");
}
return 0;
}