Codeforces 463D. Gargari and Permutations (dp,distinct下的LCS问题)

来源:互联网 发布:webshell密码暴力破解 编辑:程序博客网 时间:2024/05/21 10:22

D. Gargari and Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
4 31 4 2 34 1 2 31 2 4 3
output
3
Note

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

http://codeforces.com/contest/463/problem/D


//Hello. I'm Peter.#include<cstdio>#include<iostream>#include<sstream>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>#include<functional>#include<cctype>#include<ctime>#include<stack>#include<queue>#include<vector>#include<set>#include<map>using namespace std;typedef long long ll;typedef long double ld;#define peter cout<<"i am peter"<<endl#define input freopen("data.txt","r",stdin)#define INT (0x3f3f3f3f)*2#define LL (0x3f3f3f3f3f3f3f3f)*2#define len(a) (int)strlen(a)#define clr(a) memset(a,0,sizeof(a))#define clr_minus1(a) memset(a,-1,sizeof(a))#define clr_INT(a) memset(a,INT,sizeof(a))#define clr_true(a) memset(a,true,sizeof(a))#define clr_false(a) memset(a,false,sizeof(a))#define clr_queue(q) while(!q.empty()) q.pop()#define clr_stack(s) while(!s.empty()) s.pop()#define rep(i, a, b) for (int i = a; i < b; i++)#define dep(i, a, b) for (int i = a; i > b; i--)#define repin(i, a, b) for (int i = a; i <= b; i++)#define depin(i, a, b) for (int i = a; i >= b; i--)#define pi 3.1415926535898#define eps 1e-6#define MOD#define MAXN#define N 1024#define M 10int n,k,ans;int pos[M][N];int dp[N],a[N];bool is_ok(int x1,int x2){    repin(i,2,k)    {        if(pos[i][x1]>pos[i][x2]) return false;    }    return true;}int main(){//    input;    cin>>n>>k;    repin(i,1,k)    {        repin(j,1,n)        {            int t;            scanf("%d",&t);            pos[i][t]=j;            if(i==1) a[j]=t;        }    }    repin(i,1,n)    {        dp[i]=1;    }    repin(i,1,n)    {        repin(j,i+1,n)        {            int x1=a[i],x2=a[j];            if(is_ok(x1,x2)) dp[j]=max(dp[j],dp[i]+1);        }    }    ans=0;    repin(i,1,n)    {        ans=max(ans,dp[i]);    }    cout<<ans<<endl;}


0 0
原创粉丝点击