题目链接: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)的更多相关文章

  1. Codeforces 463D Gargari and Permutations

    http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...

  2. Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】

    题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...

  3. codeforces 463D Gargari and Permutations(dp)

    题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...

  4. 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相应的字符 ...

  5. Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题

    http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...

  6. 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 ...

  7. Codeforces 463D

    题目链接 D. Gargari and Permutations time limit per test 2 seconds memory limit per test 256 megabytes i ...

  8. CodeForces 463D DP

    Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...

  9. Maximal Area Quadrilateral CodeForces - 340B || 三点坐标求三角形面积

    Maximal Area Quadrilateral CodeForces - 340B 三点坐标求三角形面积(可以带正负,表示向量/点的不同相对位置): http://www.cnblogs.com ...

随机推荐

  1. Navicat Premium和Navicat for MySQL哪个好用?

    之前在Navicat官网下载了Navicat Premium和Navicat for MySQL使用.Navicat官网产品下载地址:https://www.navicat.com.cn/produc ...

  2. Java之HashMap用法

    源码: package test_demo; import java.util.HashMap; import java.util.Iterator; import java.util.Map; im ...

  3. poj2991 Crane(线段树)

    Description ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of variou ...

  4. google-gson 使用及GsonBuilder设置

    Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率.   从结构上看,所有的数据(data)最终都可以分解成三种类型: 第一种类型是标量(scalar),也就是一个单独的字符串( ...

  5. 【题解】 bzoj3105: [cqoi2013]新Nim游戏 (线性基+贪心)

    bzoj3105,懒得复制 Solution: 首先你要有一个前置技能:如果每堆石子异或和为\(0\),则先手比输 这题我们怎么做呢,因为我们没人要先取掉几堆,为了赢对方一定会使剩下的异或和为\(0\ ...

  6. SpringBoot基础篇AOP之基本使用姿势小结

    一般来讲,谈到Spring的特性,绕不过去的就是DI(依赖注入)和AOP(切面),在将bean的系列中,说了DI的多种使用姿势:接下来看一下AOP的玩法 <!-- more --> I. ...

  7. svn问题汇总

    1 svn图标 2 问题 SVN删除文件 一.本地删除SVN删除文件中的本地删除,指的是在客户端delete了一个文件,但还没有commit,使用revert来撤销删除. 二.服务器删除1.通过本地删 ...

  8. SQL Server 行列相互转换命令:PIVOT和UNPIVOT使用详解

    一.使用PIVOT和UNPIVOT命令的SQL Server版本要求 1.数据库的最低版本要求为SQL Server 2005 或更高. 2.必须将数据库的兼容级别设置为90 或更高. 3.查看我的数 ...

  9. spring boot 分布式事务实现(XA方式)

    关于spring boot 支持分布式事务,XA是常用的一种方式. 这里把相关的配置记下,方便以后使用. 首先配置两个不同的数据源 : 订单库.持仓库. /** * Created by zhangj ...

  10. 设计模式之Mixin模式

    介绍 mixin模式就是一些提供能够被一个或者一组子类简单继承功能的类,意在重用其功能.在面向对象的语言中,我们会通过接口继承的方式来实现功能的复用.但是在javascript中,我们没办法通过接口继 ...