[POJ1934]Trip

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

题意:
求两个数列的所有不同的最长公共子序列(保证不同的最长公共子序列数量<=1000)

题解:
动态规划记录路径,DFS回溯寻找所有公共子序列
记得可行性剪枝
数组开大一点防止RE

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<cstdio>
#define LiangJiaJun main
#define ll long long
using namespace std;
char sa[104],sb[104];
int la,lb,f[104][104];
struct Ds{
    int x,y;
};
Ds  md(int x,int y){
    Ds T;
    T.x=x;T.y=y;
    return T;
}
string alphabet[34]={
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z"
};
vector<Ds>a[104];
string s;
set<string>tr;

string tos(char r){
    return alphabet[(int)(r-'a')];
}
void dfs(int x,Ds now){
     if(x<=0){
        reverse(s.begin(),s.end());
        tr.insert(s);
        return ;
     }
     for(int i=0;i<a[x].size();i++){
         if(a[x][i].x>=now.x)break;//可行性剪枝
         if(a[x][i].x<now.x&&a[x][i].y<now.y){
            string temp=s;
            s=s+tos(sa[a[x][i].x]);
            dfs(x-1,a[x][i]);
            s=temp;
         }
     }

}
int w33ha(){
    scanf("%s",sb+1);
    tr.clear();
    for(int i=0;i<=101;i++)a[i].clear();
    la=strlen(sa+1);
    lb=strlen(sb+1);
    int lcs;
    for(int i=1;i<=la;i++){
        for(int j=1;j<=lb;j++){
            if(sa[i]==sb[j]){
                f[i][j]=f[i-1][j-1]+1;
                a[f[i][j]].push_back(md(i,j));
            }
            else{
                f[i][j]=max(f[i-1][j],f[i][j-1]);
            }
        }
    }
    lcs=f[la][lb];
    for(int i=0;i<a[lcs].size();i++){
        s=""+tos(sa[a[lcs][i].x]);
        dfs(lcs-1,a[lcs][i]);
    }
    for(set<string>::iterator it=tr.begin();it!=tr.end();it++){
        cout<<(*it)<<endl;
    }
    return 0;
}
int LiangJiaJun (){
    while(scanf("%s",sa+1)!=EOF)w33ha();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值