没啥好说的,就是公共子串的一种变形,状态定义基本一致,
就是dp[i][j],分别表示当前状态已经匹配到两个串各自的位置,转移时,看和最终串的匹配结果
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 507
using namespace std;
int t;
char s[MAX] , s1[MAX] , s2[MAX];
bool dp[MAX][MAX];
int main ( )
{
scanf ( "%d" , &t );
int c = 1;
while ( t-- )
{
scanf ( "%s" , s1+1 );
scanf ( "%s" , s2+1 );
scanf ( "%s" , s+1 );
int len1 = strlen ( s1+1 );
int len2 = strlen ( s2+1 );
int len = strlen ( s+1 );
memset ( dp , 0 , sizeof ( dp ) );
dp[0][0] = 1;
for ( int i = 1 ; i <= len ; i++ )
for ( int j = 0 ; j <= len1 ; j++ )
{
if ( j > i || i - j > len2 ) continue;
if ( s1[j] == s[i] && i-j >=0 && j > 0 && dp[j-1][i-j] )
dp[j][i-j] = 1;
if ( s2[i-j]==s[i] && i-j-1 >= 0 &&dp[j][i-j-1] )
dp[j][i-j] = 1;
}
printf ( "Data set %d: " , c++ );
if ( dp[len1][len2] ) printf ( "yes\n" );
else printf ( "no\n" );
}
}