POJ2250 & UVA 531 Compromise (string, longest common subsequence)

Source: Internet
Author: User


Compromise



Topic:




Main topic:

There are two short passages, each with several words, the longest common subsequence in the two passages, and output it!

When no essay is entered as "#", the entry of the passage is closed.

This problem is multiple sets of test data, if only one group, then WA, I because this was WA once!


The longest common sub-sequence of the solution, not much to say, basically all the algorithms are explained in the book.

This problem, test instructions and solution I think is not difficult, I personally think the difficulty is the longest common sub-sequence of the record.


For the longest common subsequence record save, I used the C + + STL string to record the save, the advantage of string is that it can be directly added to the string, so it is more convenient, for the storage of output data, I used the vector container, so that the time to read more convenient, and stored in the string type of variable, for the later answer more convenient!


Attached code:

#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm> #include <vector> #define INF 0x3f3f3f3fusing namespace Std;int main () {vector <string> str; Vector <string> str1;string str2;int n = 0,m = 0;while (cin >> str2)//Execute multiple sets of test data, this is a group of test data, write a set of results is wrong {str.clear (); Str1.clear () n = 0;m = 0;if (str2! = "#")//determine if the first input of the first article is not # if not, continue typing, instead, enter data for the second article {N++;str.push_ba        CK (STR2);        while (Cin >> str2 && str2! = "#") {N++;str.push_back (STR2); }}while (Cin >> str2 && str2! = "#") {M++;str1.push_back (str2);}           int dp[110][110] = {0};string ans[110][110];    Ans[i][j] represents the longest common subsequence that arrives at the first arrival I of the second arrival J Vector <string>:: iterator it1;   Defining iterators vector <string>:: iterator it2; Ditto int i = 0,j;for (it1 = Str.begin (); It1! = Str.end (); it1++,i++)//two-layer loop, dynamic planning solves the longest common subsequence problem {j = 0;for (It2 = str1. Begin (); It2! = Str1.end (); it2++, j + +) {if (*it1 = = *it2)//If the word is the same as add operation {dp[i + 1][j + 1] = Dp[i][j] + 1;string a;if (!ans[i][j].empt              Y ()) A + = ";      Ans is not empty, then the description is not the first one, then there is a space between the previous word and a + = *it1;  Ans[i + 1][j + 1] = Ans[i][j] + A;  Select the word with the face}else//different, assign {dp[i + 1][j + 1] = max (dp[i][j + 1],dp[i + 1][j]), ans[i + 1][j + 1] = Dp[i][j + 1] > dp[i + 1][j]? Ans[i][j + 1]: Ans[i + 1][j];}}       cout << ans[n][m] << Endl; Output}return 0;}


POJ2250 & UVA 531 Compromise (string, longest common subsequence)

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.