hdu1159
这题实在是郁闷 检查了几个小时的错误 十几次runtime 居然是没定义成全局变量
m[][]存当前最大的公共子序列
m[i][j] = m[i-1][j-1] +1 (x[i] = y[i])
else
m[i][j] = max(m[i-1][j],m[i][j-1]);
View Code
1 #include<stdio.h> 2 #include<string.h> 3 int m[1001][1001]; 4 char c1[1001], c2[1001]; 5 int main() 6 { 7 int i,j, k1, k2; 8 while(scanf("%s %s", c1, c2)!=EOF) 9 { 10 k1 = strlen(c1); 11 k2 = strlen(c2); 12 for(i = 0 ; i <= k1 ; i++) 13 m[i][0] = 0; 14 for(i = 0 ; i <= k2 ; i++) 15 m[0][i] = 0; 16 for(i = 1 ; i <= k1 ; i++) 17 { 18 for(j = 1 ; j <= k2 ; j++) 19 { 20 if(c1[i-1] == c2[j-1]) 21 m[i][j] = m[i-1][j-1]+1; 22 else 23 { 24 if(m[i-1][j]>m[i][j-1]) 25 m[i][j] = m[i-1][j]; 26 else 27 m[i][j] = m[i][j-1]; 28 } 29 } 30 } 31 printf("%d\n",m[k1][k2]); 32 } 33 return 0; 34 }