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. .Net Core Web Api 上传女朋友的照片到微软云Azure Storage

    前言 实现一个Web Api,把女朋友照片保存到Azure云的storage里. Image Upload Api 在对应的Api Controller里,加上attribute: [Consumes ...

  2. ubuntu:xxx is not in the sudoers file. 问题解决

    ubuntu 下普通用户用 sudo 执行命令时报 "xxx is not in the sudoers file.This incident will be reported" ...

  3. 物联网RFID技术之应用ETC系统

    背景 信息物理系统CPS通过集成先进的感知.计算.通 信.控制等信息技术和自动控制技术,构建了物理空间与信息空间中人. 机.物.环境.信息等要素相互映射.适时交互.高效协同的复杂系统, 实现系统内资源 ...

  4. WPF 水印TextBox WatermarkTextBox

    //https://blog.csdn.net/puchitomato/article/details/12248691 转自以上链接,自己添加了Enter响应事件.    public class ...

  5. 前后端分离密码登陆加密RSA方案(java后端)

    前言:密码加密有很多种方案,这里不做过多讨论,本篇文章是基于RSA加密实现. 首先在前端工程中需要引入加密js: "jsencrypt": "2.3.1",(注 ...

  6. 折腾Java设计模式之备忘录模式

    原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...

  7. nginx 启动错误

    场景 在Windows下 启动nginx报错: nginx: [error] ReadFile() : Incorrect function) 解决 因为 nginx.conf 中存在 /* 被认为是 ...

  8. Uncaught ReferenceError: jQuery is not defined

    页面调试时,明明引入了JQ文件,却一直提示Uncaught ReferenceError: jQuery is not defined错误. 转自:http://blog.csdn.net/baicp ...

  9. React create-react-app Build fails after eject: Cannot find module '@babel/plugin-transform-react-jsx'

    运行 npm run eject 出现报错 Build fails after eject: Cannot find module '@babel/plugin-transform-react-jsx ...

  10. 兹瓷查rank和kth的STL平衡树

    兹瓷查rank和kth的STL平衡树 明天就是一轮省选了啊..这可能是退役前的最后一篇博文了吧(如果心情不好怕是连游记都会咕) 众周所知stl中有一个依靠红黑树实现的nb数据结构-std::set 但 ...