codeforces 463D Gargari and Permutations(dp)
参考网上的代码的、、、
//要找到所有序列中的最长的公共子序列,
//定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度,
//状态转移方程为dp[i]=max(dp[i],dp[j]+1); j<i //先预处理出两个数在所有序列中的位置关系,
//例如两个数a和b,只要在任意一个序列中a在b的后面,则记after[a][b]=1。 //在递推的时候如果!after[a][b],则进行状态转移。 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ; int a[][];
int after[][];
int dp[];
int main () {
int n,k;
scanf("%d%d",&n,&k);
int ii=;
for(int i=;i<k;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
memset(after,,sizeof(after));
memset(dp,,sizeof(dp));
for(int i=;i<k;i++)
{
for(int j=;j<n;j++)
{
for(int k=j+;k<n;k++)
{
after[a[i][k]][a[i][j]]=; //存在 k在j后面
}
}
} int ans=; //直接对1~n进行状态转移不可以
//要根据第一行来dp
for(int i=;i<n;i++)
{
dp[i]=;
for(int j=;j<i;j++)
{
if(after[a[][j]][a[][i]]==)dp[i]=max(dp[i],dp[j]+);
}
ans=max(dp[i],ans);
}
printf("%d\n",ans);
return ;
}
codeforces 463D Gargari and Permutations(dp)的更多相关文章
- 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
http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...
- Codeforces 463D Gargari and Permutations(求k个序列的LCS)
题目链接:http://codeforces.com/problemset/problem/463/D 题目大意:给你k个序列(2=<k<=5),每个序列的长度为n(1<=n< ...
- 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 - 285E: Positions in Permutations(DP+组合数+容斥)
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive in ...
- CF463D Gargari and Permutations dp
给定 $n<=10$ 个 $1$~$n$ 的排列,求这些排列的 $LCS$. 考虑两个排列怎么做:以第一个序列为基准,将第二个序列的元素按照该元素在第一个序列中出现位置重新编号. 然后,求一个 ...
- Codeforces 463D
题目链接 D. Gargari and Permutations time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 【题解】POJ2279 Mr.Young′s Picture Permutations dp
[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
随机推荐
- NOJ1066-堆排序
堆排序 时间限制(普通/Java) : 1000 MS/ 3000 MS 运行内存限制 : 65536 KByte总提交 : 414 测试通过 : 220 比 ...
- MongoDB牛刀小试
MongoDB基本操作 1.MongoDB的启动 首先创建一个目录作为MongoDB的工作目录: 进入MongoDB的bin目录: 执行mongod命令,使用参数--dbpath指定MongoDB的工 ...
- hdu 4255 A Famous Grid
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...
- hdu 1429 胜利大逃亡(续)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...
- echo换行的蛋疼事
openstack@openstack:~$ echo "abc" | shasum03cfd743661f07975fa2f1220c5194cbaff48451 -而使用Ja ...
- jquery方法回到顶部代码
<style> /*默认样式,主要是position:fixed实现屏幕绝对定位*/ #gotoTop{display:none;position:fixed;top:75%;left:5 ...
- 关于自定义的NavigationBar
系统的NavigationBar局限太大,而且现在我要做的navigationBar需要四个按钮,一个Label,一个ImageView,所以不能用系统默认的. 刚刚咨询了一个高手,她的建议是,将系统 ...
- Android编程: MVC模式、应用的生命周期
学习内容:Android的应用剖析.MVC模式.应用的生命周期 ====Android的应用剖析==== 一个Android应用程序会使用如下组件: Android Activities 界面 A ...
- Hibernate从入门到精通(十)多对多单向关联映射
上一篇文章Hibernate从入门到精通(九)一对多双向关联映射中我们讲解了一下关于一对多关联映射的相关内容,这次我们继续多对多单向关联映射. 多对多单向关联映射 在讲解多对多单向关联映射之前,首先看 ...
- Jackson如何使JSON输出变得优雅?
本篇文章翻译自:How to enable pretty print JSON output (Jackson) 在这篇文章中,我们将教你如何利用Jackson Library在控制台或者JSP页面优 ...