10405-longest Common subsequence
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_ problem&problem=1346
Sequence 1:
Sequence 2: Given two sequences of character s, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences:
Abcdgh
AEDFHR
is adh of length 3.
Input consists of pairs of lines. The "the" a pair contains the "a" and the second line contains the second string. Each of the A separate line and consists of an at most 1,000 characters
For each subsequent pair of input lines, output a line containing one integer number which satisfies the criteria stated a Bove.
Sample input
a1b2c3d4e
zz1yy2xx3ww4vv
abcdgh
aedfhr
abcdefghijklmnopqrstuvwxyz
A0b0c0d0e0f0g0h0i0j0k0l0m0n0o0p0q0r0s0t0u0v0w0x0y0z0
Abcdefghijklmnzyxwvutsrqpo
Opqrstuvwxyzabcdefghijklmn
Output for the sample input
4
3
14
Template questions?
Complete code:
/*0.068s*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace Std;
const int MAXN = 1010;
Char A[MAXN], B[MAXN];
int DP[MAXN][MAXN];
int main (void)
{while
(gets (a))
{
gets (b);
int Lena = strlen (a), LenB = strlen (b);
memset (DP, 0, sizeof (DP));
for (int i = 1; I <= Lena. ++i) for
(int j = 1; j <= LenB; ++j)
dp[i][j] = (a[i-1] = = b[j-1]? dp[i- 1][j-1] + 1:max (dp[i][j-1], dp[i-1][j));
printf ("%d\n", Dp[lena][lenb]);
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/