531-compromise
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=472
Note Use a global variable flag to determine whether to output spaces.
Complete code:
/*0.029s*/#include <bits/stdc++.h> using namespace std;
Char a[105][35], b[105][35];
int dp[105][105], path[105][105];
BOOL first;///It is recommended that you adjust the output of a space with a global variable void Printlcs (int i, int j) {if (i = = 0 | | j = = 0) return;
if (path[i][j] = = 1) {Printlcs (i-1, j-1);
if (a) {printf ("%s", A[i]);
i = false;
else printf ("%s", A[i]);
else if (path[i][j] = = 2) Printlcs (I-1, J);
else Printlcs (i, j-1);
int main () {int Lena, LenB, I, J; while (~SCANF ("%s", A[1])) {if (a[1][0]!= ' # ') for (Lena = 2; scanf ("%s", A[lena)), A[lena ][0]!= ' # ';
++lena);
for (LenB = 1; scanf ("%s", B[lenb)), b[lenb][0]!= ' # '; ++lenb ';
memset (DP, 0, sizeof (DP));
for (i = 1; i < Lena. ++i) for (j = 1; j < LenB; ++j) {if (strcmp (A[i], b[j]) = = 0) Dp[i][j] = Dp[i-1][j-1] + 1, path[i][j]
= 1;
else if (Dp[i-1][j] >= dp[i][j-1]) dp[i][j] = Dp[i-1][j], path[i][j] = 2;
else dp[i][j] = dp[i][j-1], path[i][j] = 3;
} A = true;
Printlcs (Lena-1, lenb-1);
Putchar (10);
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/