1 for(int i = 1; i <= m; i ++) 2 for(int j = 1; j <= n; j ++) 3 if(!strcmp(a[i], b[j])) 4 { 5 f[i][j] = f[i-1][j-1] + 1; 6 p[i][j] = 1;//赋值 7 } 8 else if(f[i-1][j]>f[i][j-1]) 9 { 10 f[i][j] = f[i-1][j]; 11 p[i][j] = 0;//赋值 12 } 13 else 14 { 15 f[i][j] = f[i][j-1]; 16 p[i][j] = -1;//赋值 17 }
1 void print(int i , int j)//输出 2 { 3 if(!i || !j) 4 return ; 5 if(p[i][j] == 1) 6 { 7 print(i-1,j-1); 8 if(flag) 9 printf(" "); 10 else 11 flag = 1; 12 printf("%s", a[i]); 13 } 14 else if(p[i][j] == 0) 15 print(i-1,j); 16 else print(i,j-1); 17 }
时间: 2024-11-10 17:32:58