http://poj.org/problem?id=1458
2.Idea
Using dp method with 2d array.
3.Source:
const int MAX_N = 5555;
int L[MAX_N][MAX_N];
int lcs(char *x, char *y)
{
int m = strlen(x);
int n = strlen(y);
int i, j;
for (i = 0; i <= m; i++)
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) L[i][j] = 0;
else if (x[i - 1] == y[j - 1]) L[i][j] = L[i - 1][j - 1] + 1;
else L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
return L[m][n];
}
int main()
{
char x[5555], y[5555];
while (scanf("%s%s", &x,&y) != EOF) {
cout << lcs(x, y) << endl;
}
return 0;
}
No comments:
Post a Comment