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

OJ-ID:
CodeForce 113B

author:
Caution_X

date of submission:
2019-09-27

tags:
DP

description modelling:
求多个数列的LCS

major steps to solve it:
1.dp[i]:表示以数字i结尾得到的LCS,pos[i][j]表示数字j再第i个数列的位置,cnt[i]表示数字i出现了几次
2.从每个数列第一个数开始往后遍历,当cnt[i]=k时说明i可以作为LCS的一部分了
3.接下来需要讨论一下,在LCS中加入i对答案的影响
4.我们用vector<>存入所有可以作为LCS一部分的值,然后遍历vector中的数,判断二者的pos,来决定i应该插入在哪一个位置
5.遍历完成后vector<>加入i并且重新从2步骤开始

warnings:

AC code:

#include<bits/stdc++.h>
using namespace std;
int a[][];
int dp[];//以i结尾的LCS
int cnt[],pos[][];//pos[i][j]=:j在 i中出现的位置
vector<int> q;
int main()
{
//freopen("input.txt","r",stdin);
int n,k,ans=;
scanf("%d%d",&n,&k);
for(int i=;i<k;i++)
for(int j=;j<n;j++)
scanf("%d",&a[i][j]);
memset(cnt,,sizeof(cnt));
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
for(int j=;j<k;j++){
int cur=a[j][i];
pos[j][cur]=i;
cnt[cur]++;
if(cnt[cur]==k){
if(q.empty()) dp[cur]=;
else{
for(int kk=;kk<q.size();kk++){
bool flag=false;
for(int l=;l<k;l++){
if(pos[l][q[kk]]>pos[l][cur]){
flag=true;
break;
}
}
if(!flag) dp[cur]=max(dp[cur],dp[q[kk]]+);
else dp[cur]=max(dp[cur],);
}
}
ans=max(ans,dp[cur]);
q.push_back(cur);
}
}
}
printf("%d\n",ans);
return ;
}

最新文章

  1. [转载]win32 计时器使用
  2. subprocess添加超时功能
  3. EntityFramework5学习
  4. [转]用Linq取CheckBoxList選取項目的值
  5. windows下的mongodb分片配置
  6. Web应用登出后防止浏览器后退
  7. BNUOJ 26579 Andrew the Ant
  8. hadoop压缩配置
  9. UE32修改TAB键为空格键
  10. java中重载变长参数方法
  11. POI--帮助文档
  12. 使用maven搭建hibernate的pom文件配置
  13. oracle优化(一)
  14. 30.QT-渐变之QLinearGradient、 QConicalGradient、QRadialGradient
  15. vss整合配置连接到Myeclipse中以及中文配置
  16. adb命令查看app的日志
  17. Oracle GoldenGate DDL 详细说明 使用手册(较早资料)
  18. .NET Core + gRPC 实现数据串流 (Streaming)
  19. Sqli-labs less 10
  20. Flask实战第41天:发送短信验证码

热门文章

  1. Python笔记:设计模式之代理模式
  2. android studio 菜单中的app运行按钮上有个叉号,原因与解决办法(自己去百度)
  3. Zipkin客户端链路追踪源码解析
  4. grep命令提示&quot;binary file matches **.log&quot;解决方法
  5. 跳转常规 -- 为什么不要使用404、500等http状态码作为业务代码响应
  6. Mixins and Python
  7. CGROUP九大子系统
  8. unbuntu更换软件源
  9. nginx高级用法
  10. 【cf570】D. Tree Requests(dsu on tree)