Uva10405longest common subsequence

Source: Internet
Author: User

Question:
Returns the largest common subsequence (LCS)
Analysis:
It's depressing to use DP. There is a space. I used scanf to read wa, And I used gets to read.
The state transfer equation is DP [I] [J] = DP [I-1] [J-1] + 1, S1 [I] = S2 [J]
= Max {DP [I-1] [J], DP [I] [J-1]} S1 [I]! = S2 [J]

# Include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace STD;
# Define x 1005
Char s [X], CH [x];
Int DP [x] [x];
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
While (gets (s ))
{
Gets (CH );
/// // LCS Template
Int len1 = strlen (s );
Int len2 = strlen (CH );
Memset (DP, 0, sizeof (DP ));
For (INT I = 1; I <= len1; I ++)
For (Int J = 1; j <= len2; j ++)
If (s [I-1] = CH [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 [len1] [len2]);
}
Return 0;
}

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.