博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 463D Gargari and Permutations(求k个序列的LCS)
阅读量:4692 次
发布时间:2019-06-09

本文共 1461 字,大约阅读时间需要 4 分钟。

题目链接:

题目大意:

给你k个序列(2=<k<=5),每个序列的长度为n(1<=n<=1000),每个序列中的数字分别为1~n,求着k个序列的最长公共子序列是多长?
解题思路:
由于每个序列的数字分别为1~n即各不相同,所以可以用pos[i][j]记录第i个序列中j的位置。
设dp[i]表示以i结尾的最长公共子序列长度,那么我们可以按顺序遍历第一个序列的位置i,
再在第一个序列中枚举位置j(j<i),然后遍历其他序列,如果对于每个序列k都满足pos[k][a[1][i]]>pos[k][a[1][j]],
那么说明a[1][i]可以接在a[1][j]后面,dp[a[1][i]]=max(dp[a[1][i],dp[a[1][j]]+1)。
这里说明一下:按顺序遍历是为了保证dp[a[1][j]]是已经求好了的,如果直接按值来遍历则会出现前面的dp值未求好的情况。

代码:

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #define lc(a) (a<<1)15 #define rc(a) (a<<1|1)16 #define MID(a,b) ((a+b)>>1)17 #define fin(name) freopen(name,"r",stdin)18 #define fout(name) freopen(name,"w",stdout)19 #define clr(arr,val) memset(arr,val,sizeof(arr))20 #define _for(i,start,end) for(int i=start;i<=end;i++) 21 #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);22 using namespace std;23 typedef long long LL;24 const int N=2e3+5;25 const LL INF64=1e18;26 const int INF=0x3f3f3f3f;27 const double eps=1e-10;28 29 int dp[N],a[10][N],pos[10][N];//dp[i]表示以i结尾的最长公共子序列长度 30 31 int main(){32 FAST_IO;33 int n,q;34 cin>>n>>q;35 for(int i=1;i<=q;i++){36 for(int j=1;j<=n;j++){37 cin>>a[i][j];38 pos[i][a[i][j]]=j;39 }40 }41 42 for(int i=1;i<=n;i++){43 dp[a[1][i]]=1;44 for(int j=1;j

 

转载于:https://www.cnblogs.com/fu3638/p/9131480.html

你可能感兴趣的文章
JSP页面间传递参数
查看>>
VSNETcodePrint 2005 & SQL ServerPrint 2005
查看>>
java数组基本操作
查看>>
String的indexOf()用于获取字符串中某个子字符串的位置
查看>>
shell 脚本运算符
查看>>
又一道软通动力7K月薪面试题——银行业务调度系统
查看>>
Matlab画图-非常具体,非常全面
查看>>
浏览器同源策略及其规避方法
查看>>
ReactJS入门
查看>>
linux网站配置文件.htaccess伪静态转换到IIS web.config中
查看>>
CodeForces 1B
查看>>
win10应用UserControl
查看>>
Magento开发文档(二):Magento配置
查看>>
[LeetCode] 100. Same Tree Java
查看>>
BZOJ4516: [Sdoi2016]生成魔咒(后缀自动机)
查看>>
查看手机已经记住的WIFI密码
查看>>
最新版IntelliJ IDEA2019 破解教程(2019.08.07-情人节更新)
查看>>
Windows下命令(bat可用)
查看>>
我是怎么用缠论在商品里边抢钱之二 (2019-07-12 15:10:10)
查看>>
python入门之正则表达式
查看>>