Poj 1458 common subsequence (longest common subsequence LCs)

Source: Internet
Author: User

Poj1458 common subsequence (longest common subsequence LCs)

Http://poj.org/problem? Id = 1458

Question:

Here are two strings for you to find the longest common sub-sequence length of the two strings.

Analysis:

This question does not need to output subsequences. It is very simple and can be processed directly.

Set DP [I] [J] = x to indicate that the maximum length of common subsequences of the first I character of string a and the first J character of string B is X.

Initialization: DP is all 0.

Status Transfer:

IFA [I] = B [J] Then

DP [I] [J] = DP [I-1] [J-1] + 1

Else

DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1])

The above formula: When a [I] = B [J], the I character of a and the J character of B must be a [1 .. i] and B [1 .. j] in the longest common subsequence, so DP [I] [J] = DP [I-1] [J-1] + 1.

When a [I]! = B [J], at least one of a [I] and B [J] is not possible in a [1 .. i] and B [1 .. so DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1])

Final Demand: DP [N] [M].

AC code:

# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> using namespace STD; const int maxn = 1000 + 5; int n, m; int DP [maxn] [maxn]; char S1 [maxn], S2 [maxn]; int main () {While (scanf ("% S % s", S1, S2) = 2) {n = strlen (S1); // S1 string length m = strlen (S2); // S2 String Length memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; 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 [N] [m]);} return 0 ;}

 

Now the question is how to output all LCS strings in Lexicographic Order?

We can see that if we want to find the LCS of a [1. I] and B [1. J], when a [I] = B [J,

A [I] (It is also a string of B [J] characters.) This character must be selected, so we will consider a [1 .. i-1] and B [1 .. j-1] LCS can be. I wrote a DFS Method for returning all strings in reverse order, and then saved the strings into the set, which is the result of sorting by lexicographically and removing duplicates.

The DFS process is actually a reverse recursive process. the s character array stores the num characters at the end of the LCS. if a [I] = B [J], a [I] is an array of characters that need to be saved into S. if a [I]! = B [J], so we can move forward with at most two different paths. Each DFS is a feasible path and a feasible LCS will be found.

However, the above method may have many repeated strings, so the efficiency is relatively low. To improve the efficiency, You need to record the position where each character appears and make some optimizations.

The Code is as follows:

# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <string> # include <set> using namespace STD; const int maxn = 100 + 5; int n, m; int DP [maxn] [maxn]; char S1 [maxn], S2 [maxn]; set <string> st; char s [maxn]; char stmp [maxn]; int CNT; // DFS starts from the I position of the S1 string and the J position of the S2 string in reverse order. // num indicates that the end of the LCS has been identified as num characters. // All LCS are saved to the st. sort and deduplicate the final output. void DFS (int I, Int J, int num) {If (Num> = CNT) // an LCS {for (INT I = num; I> = 1; I --) STM P [num-I] = s [I]; stmp [num] = '\ 0'; string TMP (stmp); ST. insert (TMP); return;} If (S1 [I] = S2 [J]) // this character is required {s [++ num] = S1 [I]; DFS (I-1, J-1, num);} else // Case Studies {If (DP [I-1] [J]> DP [I] [J-1]) DFS (I-1, j, num); else if (DP [I-1] [J] <DP [I] [J-1]) DFS (I, J-1, num); else {DFS (I-1, j, num); DFS (I, J-1, num) ;}} int main () {While (scanf ("% S % s", S1, S2) = 2) {n = strlen (S1); // S1 string length m = strlen (S2); // S2 String Length memset (DP, 0, sizeof (DP); (Int I = 1; I <= N; I ++) for (Int J = 1; j <= m; 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 [N] [m]); CNT = DP [N] [m]; // CNT is the length of lcs dfs (N-1 m-1, 0); set <string>:: iterator it; for (IT = ST. begin (); it! = ST. End (); ++ it) cout <* It <Endl;} return 0 ;}

Poj 1458 common subsequence (longest common subsequence LCs)

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.