Gargari and Permutations

CodeForces - 463D

Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?

You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

Input

The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation.

Output

Print the length of the longest common subsequence.

Examples

Input
4 3
1 4 2 3
4 1 2 3
1 2 4 3
Output
3

Note

The answer for the first test sample is subsequence [1, 2, 3].

sol:求m个序列的lcs,似乎看上去很难得样子,实际上想到dp状态就不难了,dp[i]表示以数字 i 结尾的m个序列的lcs长度,然后判断一下是否所有数字 j 都在 i 之前就可以转移了

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,a[][N],Pos[][N];
int dp[N];
int main()
{
int i,j,k;
R(n); R(m);
for(i=;i<=m;i++)
{
for(j=;j<=n;j++)
{
R(a[i][j]); Pos[i][a[i][j]]=j;
}
}
dp[a[][]]=;
for(i=;i<=n;i++)
{
for(j=;j<i;j++)
{
bool Flag=;
for(k=;k<=m&&Flag;k++) if(Pos[k][a[][j]]>Pos[k][a[][i]]) Flag=;
if(Flag) dp[a[][i]]=max(dp[a[][i]],dp[a[][j]]+);
}
if(!dp[a[][i]]) dp[a[][i]]=;
}
int ans=;
for(i=;i<=n;i++) ans=max(ans,dp[i]);
Wl(ans);
return ;
}
/*
Input
4 3
1 4 2 3
4 1 2 3
1 2 4 3
Output
3
*/

codeforces463D的更多相关文章

随机推荐

  1. JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识

    JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...

  2. Unable to execute 'doFinal' with cipher instance [javax.crypto.Cipher@4e025e0a]

    org.apache.shiro.crypto.CryptoException: Unable to execute 'doFinal' with cipher instance [javax.cry ...

  3. 利用data-src属性 更换图片

    <div class="img_src"> <img src="./images/luo.png"></div> <u ...

  4. cesium 之三维漫游飞行效果实现篇(附源码下载)

    前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...

  5. 3D GIS 应用开发 —— 基于 Mapbox GL 的实践总结

    最近在折腾的 web 端的可视化项目,由于相关业务的需要,用到了 Mapbox 这一地图开发的神器.在此先奉上一个基于mapbox-gl实现的demo(来源:uber的deck.gl项目): 下面我们 ...

  6. deepin linux学习笔记

    目录 deepin linux学习笔记 前言 linux常用命令 ls 显示文件夹内容 cd 切换当前目录 pwd 查看当前工作目录 mkdir 新建文件夹 rm 删除文件或文件夹 mv 移动文件 c ...

  7. SQL Server服务没有自动启动原因案例分析

    这个案例是前两天出现的,一直没有时间总结,25号凌晨4点去处理数据库的故障问题.远程连上公司的局域网,psping检查发现服务器的1433端口不通,数据库连接不上,但是主机又能ping通,登录服务器检 ...

  8. SQL Server(1)数据库基础

    一.数据库能够做什么 1.存储大量的数据. 2.保持数据信息的一致.完整. 3.共享和安全. 4.通过组合分析,产生新的有用信息. 二.数据库的基本概念 1.数据库就是“数据”的“仓库”. 2.数据库 ...

  9. 海思uboot启动流程详细分析(一)

    第一阶段 start.S 首先我们可以在u-boot.lds中看到ENTRY(_start),即指定了入口_start,_start也就是整个start.S的最开始: 1. reset 在arch\a ...

  10. firewalld简介及功能

    1. firewalld简介 firewalld是CentOS7/Red Hat7的一大特性,最大的好处有两个: 第一个支持动态更新,不用重启服务: 第二个就是加入了防火墙的zone概念 firewa ...