Trip

给出一个长度为n序列\(\{a_i\}\),长度为m的序列\(\{b_i\}\),求它们的不同lcs序列按字典序输出,\(n,m\leq 80\),lcs不超过1000个,字符为26个小写字母。

注意,按照传统思路,递推+暴力方案转移+归并排序,时间复杂度\(O(80^3 \times 1000)=O(512000000)=O(5.12\times 10^8)\),必然会超时,而数据范围又很小,考虑dfs,并用递推的结果优化dfs。

为了让递推结果\(f[i][j]\)表示a序列前i长度,b序列前j长度的最长lcs能起作用,必然是从后往前搜,如果当前的状态能提供的lcs加上已经确定lcs不能为最优解的话,可以剪枝,另外,为了快速确定相同的字符,我们维护\(fa[i][j]\)表示a序列字符i的在\(1\sim j\)的最靠后的位置,fb同理,不难有

\[fa[i][j]=\max(fa[i][j-1],j(a[j]==i+96))
\]

于是维护出这两个东西,以此来dfs剪枝,就可以有很高的效率,不至于tle了。

参考代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#define il inline
#define ri register
using namespace std;
string a,b,ans[2050];
int dp[101][101],fa[101][101],
fb[101][101],tot,len;
il int max(int,int);
void dfs(int,int,string),work();
int main(){
int lsy;cin>>lsy;
while(lsy--)work(),putchar('\n');
return 0;
}
void work(){
cin>>a>>b,tot&=0;
for(int i(1),j;i<=a.size();++i)
for(j=1;j<=b.size();++j){
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
if(a[i-1]==b[j-1])dp[i][j]=max(dp[i-1][j-1]+1,dp[i][j]);
}
for(int i(1),j;i<=26;++i){
for(j=1;j<=a.size();++j)
if(a[j-1]==i+96)fa[i][j]=j;
else fa[i][j]=fa[i][j-1];
for(j=1;j<=b.size();++j)
if(b[j-1]==i+96)fb[i][j]=j;
else fb[i][j]=fb[i][j-1];
}len=dp[a.size()][b.size()];
dfs(a.size(),b.size(),""),sort(ans+1,ans+tot+1);
for(int i(1);i<=tot;++i)cout<<ans[i]<<endl;
}
void dfs(int a,int b,string s){
if(a<0||b<0)return;
if(s.size()==len)return(void)(ans[++tot]=s);
for(int i(1);i<=26;++i)
if(dp[fa[i][a]][fb[i][b]]+s.size()==len)
dfs(fa[i][a]-1,fb[i][b]-1,(char)(i+96)+s);
}
il int max(int a,int b){
return a>b?a:b;
}

Trip的更多相关文章

  1. Lesson 4 An existing trip

    Text I have just received a letter from my brother,Tim. He is in Australia. He has been there for si ...

  2. dp or 贪心 --- hdu : Road Trip

    Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...

  3. 【poj1041】 John's trip

    http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...

  4. 1301. The Trip

    A number of students are members of a club that travels annually to exotic locations. Their destinat ...

  5. 三分 --- POJ 3301 Texas Trip

    Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...

  6. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)

    Problem C: The Trip Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 19  Solved: 3[Submit][Status][Web ...

  7. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  8. URAL 1004 Sightseeing Trip(最小环)

    Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...

  9. Codeforces Round #365 (Div. 2) Mishka and trip

    Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...

  10. hdu 2425 Hiking Trip

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...

随机推荐

  1. plsql创建一个表、序列、和触发器

    plsql创建表后不能直接让id递增,因此要手动创建,下面是例子: 1.创建表 SQL: create table student(id number primary key,name varchar ...

  2. rest_framework 认证组件 权限组件

    认证组件 权限组件 一.准备内容 # models class User(models.Model): name = models.CharField(max_length=32) pwd = mod ...

  3. 从零开始搭建系统1.5——Redis安装及配置

    1.在/usr/目录下创建redis目录 [root@localhost usr]# mkdir redis 2.下载安装包 wget http://download.redis.io/release ...

  4. pip3 常用操作

    清华大学pip镜像 https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ # 设置为默认 pip install pip -U pip config set ...

  5. CentOS7.6下安装MySQL

    注:本教程使用XShell ssh到CentOS服务器,并使用root用户登录,如使用其他普通用户登录,请在命令前加sudo 1).在/usr/local/目录下(看个人情况)新建文件夹mysql用来 ...

  6. vue computed和methods 计算属性和侦听器

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. C/C++ 智能指针

    // 转载自 :https://www.cnblogs.com/wuyepeng/p/9741241.html { 为什么要使用智能指针:我们知道c++的内存管理是让很多人头疼的事,当我们写一个new ...

  8. NX二次开发-UFUN修剪体UF_MODL_trim_body

    1 NX11+VS2013 2 3 4 #include <uf.h> 5 #include <uf_modl.h> 6 7 8 UF_initialize(); 9 10 / ...

  9. Always On主辅延迟相关描述

    延迟是AlwaysOn最大的敌人之一 延迟是AlwaysON的最大敌人之一.对AlwaysON而言,其首要目标就尽量减少(无法避免)主副本.辅助副本的数据延迟,实现主副本.辅助副本的“数据同步”.只有 ...

  10. 一次修复MySQL数据库的经历

    一次修复MySQL数据库的经历 实验室服务器的硬盘满了,结果导致一个线上服务的MySQL数据库的两个表坏了.具体症状是desc cdb_searchindex显示 ERROR 1017 (HY000) ...