UVA 10405 longest Common subsequence (longest common sub-sequence)

Source: Internet
Author: User
Tags first string

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.