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 ;
}

CodeForces 463D DP的更多相关文章

  1. Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】

    题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...

  2. codeforces 463D Gargari and Permutations(dp)

    题目 参考网上的代码的... //要找到所有序列中的最长的公共子序列, //定义状态dp[i]为在第一个序列中前i个数字中的最长公共子序列的长度, //状态转移方程为dp[i]=max(dp[i],d ...

  3. codeforces的dp专题

    1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...

  4. Codeforces 463D Gargari and Permutations

    http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...

  5. Two Melodies CodeForces - 813D (DP,技巧)

    https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...

  6. Consecutive Subsequence CodeForces - 977F(dp)

    Consecutive Subsequence CodeForces - 977F 题目大意:输出一序列中的最大的连续数列的长度和与其对应的下标(连续是指 7 8 9这样的数列) 解题思路: 状态:把 ...

  7. Codeforces 463D Gargari and Permutations(求k个序列的LCS)

    题目链接:http://codeforces.com/problemset/problem/463/D 题目大意:给你k个序列(2=<k<=5),每个序列的长度为n(1<=n< ...

  8. Codeforces 463D

    题目链接 D. Gargari and Permutations time limit per test 2 seconds memory limit per test 256 megabytes i ...

  9. Codeforces 721C [dp][拓扑排序]

    /* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG), ...

随机推荐

  1. RadioButtonList

    RadioButtonList <asp:Label ID="txt_Gender" runat="server" Text="性别" ...

  2. 最全各种系统版本的XPosed框架资料下载整理

    由于XPosed在不同安卓系统版本中对应的版本不同,给很多新手造成极大困扰,本文作者经过几番努力,给大家整理了各个版本对应的xposed框架版本以及相关资料,并附上相关下载链接,希望对大伙有所帮助. ...

  3. Python3---AJAX---爬虫

    前言 该文章主要介绍面对AJAX的网页如何爬去信息,主要作用是适合刚入门爬虫查看学习 修改时间:20191219 天象独行 首先,我们先介绍一下什么是AJAX,AJAX是与服务器交换数据并跟新部分网页 ...

  4. HTML颜色名称大全

    所有浏览器支持的颜色名称,所有现代浏览器都支持以下140种颜色名称(单击颜色名称或十六进制值,以将颜色视为背景颜色以及不同的文本颜色): 有关HTML颜色的完整概述,请访问我们的颜色教程. 颜色名称 ...

  5. JS基础语法---函数---介绍、定义、函数参数、返回值

    函数: 把一坨重复的代码封装,在需要的时候直接调用即可 函数的作用: 代码的重用 函数需要先定义,然后才能使用 函数名字:要遵循驼峰命名法 函数一旦重名,后面的会把前面的函数覆盖 Ctrl +鼠标左键 ...

  6. JavaScript 正则表达式 初探

    JavaScript 正则表达式 正则表达式是构成搜索模式的字符序列 搜索模式可用于文本搜索和文本替换操作 使用正则 字符串方法 在JavaScript中,正则表达式常常用两个字符串方法: searc ...

  7. Java collection 集合类架构

    https://www.cnblogs.com/fireflyupup/p/4875130.html Collection List 在Collection的基础上引入了有序的概念,位置精确:允许相同 ...

  8. git本地忽略

    添加本地忽略文件 git update-index --assume-unchanged 忽略的文件名 恢复本地忽略文件 git update-index --no-assume-unchanged ...

  9. js对象属性方法

    window对象方法方法: 1.alert():显示带有一段消息和确认按钮的警告框 2.prompt():显示可提示用户输入的对话框 3.fonfirm():显示带有一段消息以及确认按钮和取消按钮的对 ...

  10. [20190531]ORA-600 kokasgi1故障模拟与恢复(后续).txt

    [20190531]ORA-600 kokasgi1故障模拟与恢复(后续).txt --//http://blog.itpub.net/267265/viewspace-2646340/=>[2 ...