Codeforces 463D Gargari and Permutations(求k个序列的LCS)
题目链接:http://codeforces.com/problemset/problem/463/D
题目大意:
给你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值未求好的情况。
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<string.h>
#include<cctype>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=2e3+;
const LL INF64=1e18;
const int INF=0x3f3f3f3f;
const double eps=1e-; int dp[N],a[][N],pos[][N];//dp[i]表示以i结尾的最长公共子序列长度 int main(){
FAST_IO;
int n,q;
cin>>n>>q;
for(int i=;i<=q;i++){
for(int j=;j<=n;j++){
cin>>a[i][j];
pos[i][a[i][j]]=j;
}
} for(int i=;i<=n;i++){
dp[a[][i]]=;
for(int j=;j<i;j++){
int t1=a[][i],t2=a[][j];
bool flag=true;
for(int k=;k<=q;k++){
if(pos[k][t1]<=pos[k][t2]){
flag=false;
break;
}
}
if(flag)
dp[t1]=max(dp[t1],dp[t2]+);
}
} int ans=;
for(int i=;i<=n;i++){
ans=max(ans,dp[i]);
}
cout<<ans<<endl;
return ;
}
Codeforces 463D Gargari and Permutations(求k个序列的LCS)的更多相关文章
- Codeforces 463D Gargari and Permutations
		
http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...
 - Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】
		
题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...
 - codeforces 463D Gargari and Permutations(dp)
		
题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...
 - CF 463D Gargari and Permutations [dp]
		
给出一个长为n的数列的k个排列(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符 ...
 - Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
		
http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...
 - Codeforces #264 (Div. 2) D. Gargari and Permutations
		
Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...
 - Codeforces 463D
		
题目链接 D. Gargari and Permutations time limit per test 2 seconds memory limit per test 256 megabytes i ...
 - CodeForces 463D DP
		
Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...
 - Maximal Area Quadrilateral CodeForces - 340B || 三点坐标求三角形面积
		
Maximal Area Quadrilateral CodeForces - 340B 三点坐标求三角形面积(可以带正负,表示向量/点的不同相对位置): http://www.cnblogs.com ...
 
随机推荐
- javascript面向对象系列第五篇——拖拽的实现
			
前面的话 在之前的博客中,拖拽的实现使用了面向过程的写法.本文将以面向对象的写法来实现拖拽 写法 <style> .test{height: 50px;width: 50px;backgr ...
 - CentOS系统下安装 LNAM环境
			
系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian Linux系统 需要3GB以上硬盘剩余空间 128M以上内存,Xen的需要有SWAP,OpenVZ的另外 ...
 - 等价类计数(Polya定理/Burnside引理)学习笔记
			
参考:刘汝佳<算法竞赛入门经典训练指南> 感觉是非常远古的东西了,几乎从来没有看到过需要用这个的题,还是学一发以防翻车. 置换:排列的一一映射.置换乘法相当于函数复合.满足结合律,不满足交 ...
 - django-pure-pagination 分页插件
			
官网地址:https://github.com/jamespacileo/django-pure-pagination 官网上有详细的安装和使用介绍
 - shell jq
			
Mark 下,周末来补充 参考资料: https://stedolan.github.io/jq/tutorial/
 - centos6.x下安装maven
			
转自:http://www.centoscn.com/image-text/install/2014/0507/2923.html 1.下载maven包首先从官网上 http://maven.apac ...
 - 用python处理文本,本地文件系统以及使用数据库的知识基础
			
主要是想通过python之流的脚本语言来进行文件系统的遍历,处理文本以及使用简易数据库的操作. 本文基于陈皓的:<程序员技术练级攻略> 一.Python csv 对于电子表格和数据库导出文 ...
 - javascript私有方法揭示为公有方法
			
揭示模式可用于将私有方法暴露为公共方法.当为了对象的运转而将所有功能放置在一个对象中以及想尽可能地保护该对象,这种揭示模式就非常有用. 板栗: var myarray; (function(){ va ...
 - 20155304 2016-2017-2 《Java程序设计》第七周学习总结
			
20155304 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 1.时间的度量: 格林威治标准时间(GMT)通过观察太阳而得,其正午是太阳抵达天空最高点之时 ...
 - shell 流程结构
			
if 判断语句 if [ $a == $b ] then echo "等于" else echo "不等于" fi case分支选择 case $xs in ) ...