Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴一个ac代码,数组开500就够了,动态规划分解问题#include <stdio.h> #include <string.h> #define N 500 void LCSLength(int m, int n, char x[], char y[],int c[][N],int b[][N]) { int i, j; for(i = 1; i <= m; i++) c[i][0] = 0; for(i = 0; i <= n; i++) c[0][i] = 0; for(i = 1; i <= m; i++) for(j = 1; j<=n; j++) { if(x[i-1]==y[j-1]) { c[i][j] = c[i-1][j-1] + 1; b[i][j] = 1; }else if(c[i-1][j]>=c[i][j-1]){ c[i][j] = c[i-1][j]; b[i][j] = 2; }else{ c[i][j] = c[i][j-1]; b[i][j] = 3; } } } int main() { char x[N]; char y[N]; int c[N][N], b[N][N]; int m, n ; while(scanf("%s %s", x, y) !=EOF) { m = strlen(x); n = strlen(y); LCSLength(m,n, x, y, c, b); printf("%d\n", c[m][n]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator