Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13940 | Accepted: 4871 |
Description
Input
Output
Sample Input
3 cat tree tcraete cat tree catrtee cat tree cttaree
Sample Output
Data set 1: yes Data set 2: yes Data set 3: no
dp[i][j]为str1中前i个字符和str2中前j个字符能否和str3中前i+j个匹配,如果能为1,否则为0;
动态方程:a.dp[i-1][j]==1&&str1[i-1]==str3[i+j-1]//如果str1前i-1个加上str2前j个是和str3前i+j-1个是匹配的,那么考虑str1第i-1个字符和str3中第i+j-1个字符。
#include <stdio.h> #include <math.h> #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <string> #include <cstring> #include <cmath> #define setbit(x,y) x|=(1<<(y)) //将X的第Y位置1 #define clrbit(x,y) x&=~(1<<(y)) //将X的第Y位清0 #define sf scanf #define pf printf #define INF 1 << 29 #define eps 1e-6 const double PI = acos(-1.0); #define lint __int64 #define LL long long #define MAX 1e9 + 7 #define maxn 40005 //101^110=011 异或 #define ULLint unsigned long long //2^64-1>1.8*10^19 #define clr(x) memset(x, 0, sizeof(x)) #define Clr(x) memset(x, -1, sizeof(x)) using namespace std; #define SIZE 205 int dp[ SIZE ][ SIZE ]; string str1, str2, str3; void Input(){ cin >> str1 >> str2 >> str3; } bool Solution(){ memset( dp, 0, sizeof( dp ) ); dp[ 0 ][ 0 ] = 1; for ( int i = 0; i <= str1.size(); ++ i ) for ( int j = 0; j <= str2.size(); ++ j ){ if ( i > 0 && dp[ i - 1 ][ j ] && str1[ i - 1 ] == str3[ i + j - 1 ] ) dp[ i ][ j ] = 1; if ( j > 0 && dp[ i ][ j - 1 ] && str2[ j - 1 ] == str3[ i + j - 1 ] ) dp[ i ][ j ] = 1; } if ( dp[ str1.size() ][ str2.size() ] ) return true; return false; } void Output( int cas ){ printf( "Data set %d: ", cas ); if ( Solution() ) printf( "yes\n" ); else printf( "no\n" ); } int main(){ int T; int cas = 1; scanf( "%d", &T ); while ( T -- ){ Input(); Output( cas ++ ); } return 0; }