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. [Javascript] js的类和对象

    类 graph LR 类-->构造函数 类-->prototype对象 类-->instanceof运算符 类-->constructor属性 类-->isPrototy ...

  2. c#用winform开发一个简易双色球项目

    开始画面 抽奖中: 抽奖结果: 需要一个随机数Random的帮助类,让随机数唯一性 public class RandomHelper { public int GetNum(int min, int ...

  3. js或jquery实现点击某个按钮或元素显示div,点击页面其他任何地方隐藏div

    点击某个元素显示div,点击页面其他任何地方隐藏div,可用javascript和jquery两种方法实现: 一:javascript实现方法技巧<script>//定义stopPropa ...

  4. Nginx + Keepalived实现应用高可用负载均衡功能

    说明:此处仅介绍 Keepalived 实现nginx负载均衡器的高可用,关于nginx介绍和负载均衡实现可查看我的另两篇博文 Nginx负载均衡 和 Nginx配置了解 应用背景:实现高可用,避免单 ...

  5. html&css学习一

    HTML标签 HTML骨架 <HTML> <head> <title></title> </head> <body> </ ...

  6. js 3D旋转效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 第八课 表格 html5学习3

    表格用来处理表格式数据的,不是用来布局的. 一.基本语法格式 <table> <tr> 行标签 <td></td> 单元格标签 </tr> ...

  8. Android设计模式总结

    1.复合模式:三层架构.MVC.MVP.MVVM 2.设计模式-单例模式 配置类的使用. 3.设计模式-模板方法 通过抽象类或接口提前定义要实现的方法. 4.设计模式-观察者模式 消息的通知. 5.设 ...

  9. SQL Server创建Job, 实现执行相同脚本而产生不同作业计划的探究

    1 . 背景描述 本公司的SQL Server 服务器近百台,为了收集服务器运行的状态,需要在各个实例上部署监控Job,将收集到的信息推送到中央管理服务器. 收集的信息主要包括:慢查询.阻塞.资源等待 ...

  10. samba介绍和安装

    samba基本介绍 为什么需要samba 早期网络文件数据在不同主机之间传输大都可以使用Ftp完成,不过ftp使用有个小小的问题,它不能让你之间修改主机上的文件.要想修改必须要通过下载——修改——上传 ...