Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
4 2 0
最长公共子串问题:
1 #include<stdio.h> 2 #include<string.h> 3 #define maxn 1010 4 5 int d[2][maxn]; 6 char a[maxn], b[maxn]; 7 int main() 8 { 9 while(scanf("%s %s", a, b) == 2){ 10 int la, lb, i, j; 11 la = strlen(a); 12 lb = strlen(b); 13 memset(d, 0, sizeof(d)); 14 for(i = 1; i <= la; i++) 15 for(j = 1; j <= lb; j++) 16 if(a[i-1] == b[j-1]) 17 d[i%2][j] = d[(i-1)%2][j-1] + 1; 18 else 19 d[i%2][j] = d[i%2][j-1] > d[(i-1)%2][j] ? d[i%2][j-1] : d[(i-1)%2][j];//取最大值 20 printf("%d ", d[la%2][lb]); 21 } 22 return 0; 23 }
另解:
1 #include <stdio.h> 2 #include <string.h> 3 #define MAXN 1010 4 5 char X[MAXN]; 6 char Y[MAXN]; 7 int dp[MAXN][MAXN]; 8 int fmax(int a, int b) 9 { 10 return a > b ? a : b; 11 } 12 13 void dpf(int m,int n) 14 { 15 int i, j; 16 for (i = 1; i <= m; i++) 17 dp[i][0] = 0; 18 for (i = 1; i <= n; i++) 19 dp[0][i] = 0; 20 for (i = 1; i <= m; i++) 21 for (j = 1; j <= n; j++) 22 if ( X[i] == Y[j]) 23 dp[i][j] = dp[i-1][j-1]+1; 24 else 25 dp[i][j] = fmax(dp[i-1][j], dp[i][j-1]); 26 } 27 28 int main() 29 { 30 int m, n; 31 while(scanf("%s %s", X, Y) == 2){ 32 m = strlen(X); 33 n = strlen(Y); 34 memset(dp,0,sizeof(dp)); 35 dpf(m, n); 36 printf("%d ",dp[m][n]); 37 } 38 return 0; 39 }
关于最长公共子序列的进一步探讨——输出
1 #include <stdio.h> 2 #include <string.h> 3 4 int x[100], y[100], c[100][100], b[100][100]; 5 void LcsLength(int *x,int *y,int m,int n,int c[][100],int b[][100]) 6 { 7 for(int i = 0; i <= m; i++) 8 c[i][0] = 0; 9 for(int j = 0; j <= n; j++) 10 c[0][j] = 0; 11 for(int i = 1; i <= m; i++) 12 for(int j = 1; j <= n; j++){ 13 if(x[i-1] == y[j-1]){ 14 c[i][j] = c[i-1][j-1]+1; 15 b[i][j] = 0; 16 } 17 else if(c[i-1][j] >= c[i][j-1]){ 18 c[i][j] = c[i-1][j]; 19 b[i][j] = 1; 20 } 21 else{ 22 c[i][j] = c[i][j-1]; 23 b[i][j] = -1; 24 } 25 } 26 } 27 28 void PrintLCS(int b[][100], int *x, int i, int j) 29 { 30 if(i == 0 || j == 0) 31 return; 32 if(b[i][j] == 0){ 33 PrintLCS(b, x, i-1, j-1); 34 printf("%d ", x[i-1]); 35 } 36 else if(b[i][j] == 1) 37 PrintLCS(b, x, i-1, j); 38 else 39 PrintLCS(b, x, i, j-1); 40 } 41 42 int main() 43 { 44 int m, n; 45 scanf("%d", &m); 46 for(int i = 0; i < m; i++) 47 scanf("%d", &x[i]); 48 scanf("%d", &n); 49 for(int i = 0; i < n; i++) 50 scanf("%d", &y[i]); 51 LcsLength(x, y, m, n, c, b); 52 printf("最长子序列为:"); 53 PrintLCS(b, x, m, n); 54 printf(" "); 55 printf("最长子序列长度为:%d ", c[m][n]); 56 return 0; 57 }