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同理,不难有
\]
于是维护出这两个东西,以此来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的更多相关文章
- 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 ...
- dp or 贪心 --- hdu : Road Trip
Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...
- 【poj1041】 John's trip
http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...
- 1301. The Trip
A number of students are members of a club that travels annually to exotic locations. Their destinat ...
- 三分 --- POJ 3301 Texas Trip
Texas Trip Problem's Link: http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)
Problem C: The Trip Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 19 Solved: 3[Submit][Status][Web ...
- hdu 3018 Ant Trip 欧拉回路+并查集
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- URAL 1004 Sightseeing Trip(最小环)
Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...
- Codeforces Round #365 (Div. 2) Mishka and trip
Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...
- hdu 2425 Hiking Trip
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...
随机推荐
- CSS中block,inline和block-inline的区别(转载)
http://www.cnblogs.com/KeithWang/p/3139517.html 总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level e ...
- 十分钟学习 react配套的类型检测库——prop-types的运用
js 有时在定义变量的类型为number 或string 时并不会报错,所以prop-types 是专门用来检测react ,以前的版本是把它放到react架构里面 ,现在作为一个独立的库搬出来了,跟 ...
- vue与webpack
由于最近在vue-cli生成的webpack模板项目的基础上写一个小东西,开发过程中需要改动到build和config里面一些相关的配置,所以刚好趁此机会将所有配置文件看一遍,理一理思路,也便于以后修 ...
- linux每日命令(1):gzip命令
gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. gzip不仅可以用来压缩大的.较少使用的文件以节省磁盘空间,还可以和tar命令一起构成Linux操作系统中比较流 ...
- Unicode - 16 位统一超级字符集
描述 (DESCRIPTION) 国际标准 ISO 10646 定义了 通用字符集 (Universal Character Set, UCS). UCS 包含所有别的字符集标准里的字符,并且保证了 ...
- C#编程入门--MYSQLHELPER
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 随笔记录 重置root密码 2019.8.7
方法1:进入单用户模式 1.开机进入以下界面选择要启动的系统按e 2.找到星号行在后面添加上init=/bin/sh 3.按住Ctrl+x执行 4.进入单用户模式 5.如果passwd命令失败,可以直 ...
- scip 练习2.18
(define (append a b ) (if (null? a )b (cons (car a) (append (cdr a) b)))) (define (reverse l) (appen ...
- CAS -- ABA问题的解决方案
我们现在来说什么是ABA问题.假设内存中有一个值为A的变量,存储在地址V中. 此时有三个线程想使用CAS的方式更新这个变量的值,每个线程的执行时间有略微偏差.线程1和线程2已经获取当前值,线程3还未获 ...
- vue 单元素过渡
demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...