码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1934:Trip(LCS)

时间:2015-05-08 18:08:12      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:poj

Description

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

Input

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

Output

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

Sample Input

abcabcaa
acbacba

Sample Output

ababa
abaca
abcba
acaba
acaca
acbaa
acbca

Source



题意:两个字符串,按字典序输出其所有的LCS

思路:先求出LCS的长度,vis[i][j]记录到i位置时,以j结尾的最后一个位置,然后再从尾往前比较,得出所有LCS,使用set存储,可以自动排序

#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 long
const double pi = acos(-1.0);
#define N 105
#define mod 19999997
const int INF = 0x3f3f3f3f;
#define exp 1e-8

char str1[N],str2[N],tmp[N];
int dp[N][N],len1,len2,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];
            }
        }
        up(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,'\0');
        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)

标签:poj

原文地址:http://blog.csdn.net/libin56842/article/details/45583349

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!