Poj1934:trip (LCS)

Source: Internet
Author: User

Description

Alice and Bob want to go on holiday. Each of them have planned a route, which is a list of the cities to being visited in a given order. A route may contain a city more than once.
As they want to travel together, they has to agree on a common route. None wants to change the order of the cities on his or hers route or add other cities. Therefore they has no choice but to remove some cities from the route. Of course the common route should be as long as possible.
There is exactly-cities in the region. Therefore They is encoded on the lists as lower case letters from ' a ' to ' Z '.

Input

The input consists of lines; The first line is Alice's list, the second line is Bob's list.
Each list consists of 1 to lower case letters with no spaces inbetween.

Output

The output should contain all routes this meet the conditions described above, but no route should is listed more than ONC E. Each route should is printed on a separate line. There is at least one such non-empty route, but never more than, different ones. Output them in ascending order.

Sample Input

Abcabcaaacbacba

Sample Output

Ababaabacaabcbaacabaacacaacbaaacbca

Source

CEOI 2003

Test instructions: Two strings, output all of their LCS in a dictionary order
Idea: First find out the length of the LCS, Vis[i][j] record to I position, with J end of the last position, and then from the tail forward comparison, to obtain all LCS, using set storage, can be automatically sorted
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm >using namespace std, #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x; i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define LL long longconst double pi = acos (-1.0); # Define N 105#define mod 19999997const int INF = 0x3f3f3f3f; #define EXP 1e-8char Str1[n],str2[n],tmp[n];int dp[n][n],len1,l    En2,len,vis1[105][30],vis2[105][30];set<string> ans;void LCS () {int i,j;    MEM (dp,0);            Up (I,1,LEN1) {up (j,1,len2) {if (str1[i]==str2[j]) dp[i][j]=dp[i-1][j-1]+1;        else dp[i][j] = max (Dp[i][j],max (dp[i-1][j],dp[i][j-1)); }} len = Dp[len1][len2];}    void Solve (int l1,int l2,int len) {int i,p1,p2;   if (len<=0) {Ans.insert (&tmp[1]);    return;            } if (l1>0 && l2>0) {up (i,0,25) {p1 = vis1[l1][i];            P2 = vis2[l2][i];                if (dp[p1][p2]==len) {tmp[len]=i+ ' a ';            Solve (p1-1,p2-1,len-1);    }}}}int Main () {int i,j,k;        W (~scanf ("%s%s", str1+1,str2+1)) {len1 = strlen (str1+1);        Len2 = strlen (str2+1);        LCS ();        MEM (vis1,0);        MEM (vis2,0); Up (I,1,LEN1) {up (j,0,25) {if (str1[i]==j+ ' a ') vis1[i][j]=i                ;            else Vis1[i][j]=vis1[i-1][j];                    }} (I,1,len2) {up (j,0,25) {if (str2[i]==j+ ' a ')                Vis2[i][j]=i;            else Vis2[i][j]=vis2[i-1][j];        }} MEM (tmp, ' n ');        Solve (Len1,len2,len); set<String>::iterator it;    for (it = Ans.begin (); It!=ans.end (); it++) printf ("%s\n", (*it). C_STR ()); } return 0;}


Poj1934:trip (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.