junior19

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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


posted on 2017-02-22 19:38  junior19  阅读(229)  评论(0编辑  收藏  举报