POJ1934:Trip(LCS + 所有路径打印)

reference:http://blog.csdn.net/libin56842/article/details/45583349

Trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3361 Accepted: 870

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

题意:将最长公共子序列的所有情况输出。

思路:参考别人的代码,使用辅助二维数组记录每个字符在数组中出现的最后一个位置,使用set存储结果。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <set>
# include <algorithm>
using namespace std;
char a[83], b[83], s[83];
int dp[83][83], l[83][83], r[83][83], max_len, alen, blen;
set<string>se;
void dfs(int x, int y, int icount)
{
    if(icount <= 0)
    {
        se.insert(&s[1]);
        return;
    }
    if(x>0 && y>0)
    for(int i=0; i<=25; ++i)
    {
        int p1 = l[x][i];
        int p2 = r[y][i];
        if(dp[p1][p2] == icount)
        {
            s[icount] = i+'a';
            dfs(p1-1, p2-1, icount-1);
        }
    }
}

void solve(int al, int bl)
{
    memset(l, 0, sizeof(l));
    memset(r, 0, sizeof(r));
    for(int i=1; i<=al; ++i)
        for(int j=0; j<=25; ++j)
        {
            if(a[i]==j+'a')
                l[i][j] = i;
            else
                l[i][j] = l[i-1][j];
        }
    for(int i=1; i<=bl; ++i)
        for(int j=0; j<=25; ++j)
        {
            if(b[i]==j+'a')
                r[i][j] = i;
            else
                r[i][j] = r[i-1][j];
        }
}
int main()
{
    while(~scanf("%s%s",a+1,b+1))
    {
        memset(s, 0, sizeof(s));
        memset(r, 0, sizeof(r));
        memset(dp, 0, sizeof(dp));
        alen = strlen(a+1);
        blen = strlen(b+1);
        for(int i=1; i<=alen; ++i)
            for(int j=1; j<=blen; ++j)
            {
                if(a[i]==b[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]));
            }
        max_len = dp[alen][blen];
        solve(alen, blen);
        dfs(alen, blen, max_len);
        set<string>::iterator it;
        for(it = se.begin(); it!=se.end(); it++)
            printf("%s\n",(*it).c_str());
    }
    return 0;
}


转载于:https://www.cnblogs.com/junior19/p/6729982.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值