Codeforces 463D. Gargari and Permutations (dp,distinct下的LCS问题)
来源:互联网 发布:webshell密码暴力破解 编辑:程序博客网 时间:2024/05/21 10:22
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
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.
Print the length of the longest common subsequence.
4 31 4 2 34 1 2 31 2 4 3
3
The answer for the first test sample is subsequence [1, 2, 3].
//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;}
- Codeforces 463D. Gargari and Permutations (dp,distinct下的LCS问题)
- Codeforces 463D Gargari and Permutations DP(LCS变形)
- Codeforces 463D. Gargari and Permutations【DP】
- 【CodeForces】463D Gargari and Permutations DP
- codeforces 463D D. Gargari and Permutations(dp)
- Codeforces 463D Gargari and Permutations(BFS)
- codeforces 463D Gargari and Permutations
- Codeforces 463D Gargari and Permutations
- Codeforces 463D Gargari and Permutations(BFS)
- CF 463D Gargari and Permutations (dp)
- CF 463D Gargari and Permutations [dp]
- 【CODEFORCES】 D. Gargari and Permutations
- Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
- Codeforces #264 (Div. 2) D. Gargari and Permutations
- Codeforces #264 (Div. 2) D. Gargari and Permutations
- Codeforces #264 div.2 D. Gargari and Permutations
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces 264 (Div. 2) D. Gargari and Permutations
- iOS UI核心
- RocketMQ原理解析-producer 2.如何发送消息
- cocos2d-x 3.2 |飞机大战:敌机与爆炸
- 今天查资料的时候 看到的一句话 ~~~
- 分类方法
- Codeforces 463D. Gargari and Permutations (dp,distinct下的LCS问题)
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 04.游戏界面 主角飞机的添加
- 1022. Packets
- 先记下学习 —— 他山之石
- 内存管理(1)
- 复变函数及应用 第三章学习感受
- MyEclipse常用设置图解之二
- 内存管理(2)
- 开通