http://poj.org/problem?id=2192
题意:
给出3个字符串,判断第1个和第2个能否组成第3个,要求是每个字符串的相对顺序不能改变。
思路:
d[i][j]表示第1个取第i个字符,第2个取第j个字符时能否组成第3个字符。
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace std; 7 8 const int maxn = 500 + 5; 9 10 char s1[maxn], s2[maxn], s3[maxn]; 11 int d[maxn][maxn]; 12 13 int main() 14 { 15 //freopen("D:\txt.txt", "r", stdin); 16 int T; 17 scanf("%d", &T); 18 int kase = 0; 19 while (T--) 20 { 21 scanf("%s%s%s", s1+1, s2+1, s3+1); 22 int len1 = strlen(s1+1); 23 int len2 = strlen(s2+1); 24 int len3 = strlen(s3+1); 25 memset(d, 0, sizeof(d)); 26 for (int i = 1; i <= len1; i++) 27 { 28 if (s1[i] == s3[i]) 29 d[i][0] = 1; 30 else break; 31 } 32 for (int i = 1; i <= len2; i++) 33 { 34 if (s2[i] == s3[i]) 35 d[0][i] = 1; 36 else break; 37 } 38 for (int i = 1; i <= len1; i++) 39 for (int j = 1; j <= len2; j++) 40 { 41 if (s1[i] == s3[i + j] && d[i - 1][j] == 1) 42 d[i][j] = 1; 43 if (s2[j] == s3[i + j] && d[i][j - 1] == 1) 44 d[i][j] = 1; 45 } 46 printf("Data set %d: ", ++kase); 47 if (d[len1][len2] == 1) 48 printf("yes "); 49 else 50 printf("no "); 51 } 52 }