UVA 10405 longest Common subsequence
Sequence 1:
Sequence 2:
Given sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following:
Abcdgh
Aedfhr
is adh of length 3.
Input consists of pairs of lines. The first line of a pair contains the first string and the second line contains the second string. Each string was on a separate line and consists of in most 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
26
14
Title: The longest common subsequence. Problem-solving ideas: recursion.
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#define N 1005using namespace STD;CharS1[n], s2[n];intDp[n][n];intMain () { while(Gets (S1)) {gets (S2);intL1 =strlen(S1), L2 =strlen(S2);memset(DP,0,sizeof(DP)); for(inti =1; I <= L1; i++) { for(intj =1; J <= L2; J + +) {if(S1[i-1] = = S2[j-1]) {Dp[i][j] = dp[i-1][j-1] +1; }Else{Dp[i][j] = max (Dp[i-1][J], Dp[i][j-1]); } } }printf("%d\n", Dp[l1][l2]); }return 0;}
UVA 10405 longest Common subsequence (longest common sub-sequence)