HDU 1503 advanced fruits (LCS deformation and output solution)

Source: Internet
Author: User
Tags first string

HDU 1503 advanced fruits (LCS deformation and output solution)

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1503

Question:

Give you two strings S1 and S2. You need to output them and string S. s1 is a sub-sequence of S, S2 is a sub-sequence of S, and s is the shortest string that meets the preceding requirements.

Analysis:

For example, DP [I] [J] = X indicates that the length of the LCS string consisting of the first I character of the S1 string and the first J character of the S2 string is X.

We first find the DP array value of LCS, and then follow the poj2250:

Http://blog.csdn.net/u013480600/article/details/40743953

Similar to the DFS process, the S string can be output.

The general idea is:

Assume that DFS (I, j) indicates that the output I characters (Count from 0) And a string of J characters.

If I = 0 or J = 0, output the remaining string and return.

Optional (The following three processes ensure that I> 0 and j> 0):

If s [I-1] = s [J-1], then first DFS (I-1, J-1), then output S1 [I-1] (The I character of the first string.

If s [I-1]! When = s [J-1:

If DP [I-1] [J]> DP [I] [J-1], DFS (I-1, J), then output S1 [I-1.

If DP [I-1] [J] <= DP [I] [J-1], DFS (I, J-1), then output S2 [J-1.

(Carefully understand the above process)

AC code:

# Include <cstdio> # include <algorithm> # include <cstring> using namespace STD; const int maxn = 100 + 5; int n, m; int DP [maxn] [maxn]; char S1 [maxn], S2 [maxn]; void DFS (int I, Int J) {if (I + J = 0) return; else if (I = 0) // The S1 string has been output {for (int K = 0; k <j; k ++) printf ("% C ", s2 [k]); return;} else if (j = 0) // The S2 string has been output {for (int K = 0; k <I; k ++) printf ("% C", S1 [k]); return;} // The recursion below guarantees that I> 0 and j> 0. if (S1 [I-1] = S2 [J-1]) DFS (I-1, J-1), printf ("% C", S1 [I-1]); else if (DP [I-1] [J]> DP [I] [J-1]) DFS (I-1, J), printf ("% C", S1 [I-1]); else DFS (I, J-1), printf ("% C", S2 [J-1]);} int main () {While (scanf ("% S % s", S1, s2) = 2) {// initialize n = strlen (S1); M = strlen (S2); memset (DP, 0, sizeof (DP )); // recursive 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]);} // DFS + output DFS (n, m); printf ("\ n");} return 0 ;}

HDU 1503 advanced fruits (LCS deformation and output solution)

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.