POJ1934:Trip(LCS)

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值