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 foundk permutations. Each of them consists of numbers1, 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 andk
(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the nextk 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
  1. 4 3
  2. 1 4 2 3
  3. 4 1 2 3
  4. 1 2 4 3
Output
  1. 3
Note

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

题意:求k个长度为n的最长公共子序列

思路1:保存每一个数在各自串的位置,由于结果是第1个串中的某个可能,所以我们枚举第1个串的可能,然后检查假设一个以a[j]为结束的最长公共子序列成立的情况是,对于每一个串的a[i]都在a[j]的前面,那么就有dp[j] = max(dp[j], dp[i]+1)

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. using namespace std;
  6. const int maxn = 1010;
  7.  
  8. int n, k;
  9. int a[maxn][maxn], b[maxn][maxn], dp[maxn];
  10.  
  11. int check(int x, int y) {
  12. for (int i = 2; i <= k; i++)
  13. if (b[i][x] > b[i][y])
  14. return 0;
  15. return 1;
  16. }
  17.  
  18. int main() {
  19. scanf("%d%d", &n, &k);
  20. for (int i = 1; i <= k; i++)
  21. for (int j = 1; j <= n; j++) {
  22. scanf("%d", &a[i][j]);
  23. b[i][a[i][j]] = j;
  24. }
  25.  
  26. for (int i = 1; i <= n; i++)
  27. dp[i] = 1;
  28.  
  29. int ans = 0;
  30. for (int i = 1; i <= n; i++) {
  31. for (int j = i+1; j <= n; j++) {
  32. if (check(a[1][i], a[1][j]))
  33. dp[j] = max(dp[i]+1, dp[j]);
  34. }
  35. }
  36.  
  37. for (int i = 1; i <= n; i++)
  38. ans = max(ans, dp[i]);
  39. printf("%d\n", ans);
  40. return 0;
  41. }

思路2:假设一个数字i在每一个串的位置都在j前面,那么i到j就有一条有向边,那么题目就转换为DAG求最长

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <vector>
  6. using namespace std;
  7. const int maxn = 1010;
  8.  
  9. int num[10][maxn], vis[maxn];
  10. int n, k;
  11. vector<int> g[maxn];
  12.  
  13. int check(int x, int y) {
  14. for (int i = 0; i < k; i++)
  15. if (num[i][x] >= num[i][y])
  16. return 0;
  17. return 1;
  18. }
  19.  
  20. int dfs(int x) {
  21. int ans = 0;
  22. if (vis[x])
  23. return vis[x];
  24.  
  25. int size = g[x].size();
  26. for (int i = 0; i < size; i++)
  27. ans = max(ans, dfs(g[x][i]));
  28.  
  29. return vis[x] = ans + 1;
  30. }
  31.  
  32. int main() {
  33. scanf("%d%d", &n, &k);
  34. memset(vis, 0, sizeof(vis));
  35. for (int i = 0; i <= n; i++)
  36. g[i].clear();
  37.  
  38. int a;
  39. for (int i = 0; i < k; i++)
  40. for (int j = 1; j <= n; j++) {
  41. scanf("%d", &a);
  42. num[i][a] = j;
  43. }
  44.  
  45. for (int i = 1; i <= n; i++)
  46. for (int j = 1; j <= n; j++)
  47. if (check(i, j))
  48. g[i].push_back(j);
  49.  
  50. int ans = 0;
  51. for (int i = 1; i <= n; i++)
  52. if (!vis[i])
  53. ans = max(ans, dfs(i));
  54.  
  55. printf("%d\n", ans);
  56. return 0;
  57. }

Codeforces #264 (Div. 2) D. Gargari and Permutations的更多相关文章

  1. Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题

    http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...

  2. Codeforces Round #264 (Div. 2) C. Gargari and Bishops 主教攻击

    http://codeforces.com/contest/463/problem/C 在一个n∗n的国际象棋的棋盘上放两个主教,要求不能有位置同时被两个主教攻击到,然后被一个主教攻击到的位置上获得得 ...

  3. Codeforces Round #264 (Div. 2) C Gargari and Bishops 【暴力】

    称号: 意甲冠军:给定一个矩阵,每格我们有一个数,然后把两个大象,我希望能够吃的对角线上的所有数字.我问两个最大的大象可以吃值. 分析:这种想法是暴力的主题,计算出每一格放象的话能得到多少钱,然后求出 ...

  4. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

  5. Codeforces #344 Div.2

    Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...

  6. Codeforces #345 Div.1

    Codeforces #345 Div.1 打CF有助于提高做题的正确率. Watchmen 题目描述:求欧拉距离等于曼哈顿距离的点对个数. solution 签到题,其实就是求有多少对点在同一行或同 ...

  7. Codeforces Beta Round #27 (Codeforces format, Div. 2)

    Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...

  8. Codeforces#441 Div.2 四小题

    Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...

  9. codeforces #592(Div.2)

    codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend ...

随机推荐

  1. Linux下交叉编译gdb,gdbserver+gdb的使用以及通过gdb调试core文件

    交叉编译gdb和gdbserver 1.下载gdb:下载地址为:http://ftp.gnu.org/gnu/gdb/按照一般的想法,最新版本越好,因此下载7.2这个版本.当然,凡事无绝对.我们以gd ...

  2. word vba 1 页面视图

  3. error C2220: warning treated as error - no 'object' file generated warning C4819: The file contains a character that cannot be represented in the current code page (936).

    用Visual Studio2015 编译时,遇到如下编译错误: error C2220: warning treated as error - no 'object' file generated ...

  4. [D3] Load and Inspect Data with D3 v4

    You probably use a framework or standalone library to load data into your apps, but what if that’s o ...

  5. Cygwin 与 MinGW/MSYS/MSYS2,如何选择?甚至还有GNU utilities for Win32

    Cygwin与MinGW/MSYS,如何选择? 2012-11-03更新:加入 MSYS 的内容. 2013-10-15更新:修改表格格式,加入介绍链接. 2014-12-17更新:加入 MSYS2 ...

  6. Android 控件EditText的setOnEditorActionListener方法的理解

    需要注意的是 setOnEditorActionListener这个方法,并不是在我们点击EditText的时候触发,也不是在我们对EditText进行编辑时触发,而是在我们编辑完之后点击软键盘上的回 ...

  7. IdentityServer4实战 - JWT Token Issuer 详解

    原文:IdentityServer4实战 - JWT Token Issuer 详解 一.前言 本文为系列补坑之作,拖了许久决定先把坑填完. 下文演示所用代码采用的 IdentityServer4 版 ...

  8. Java 学习(22):Java MySQL 连接

    Java MySQL 连接 本章节我们为大家介绍 Java 如何使用 使用 JDBC 连接 MySQL 数据库. Java 连接 MySQL 需要驱动包,最新版下载地址为:http://dev.mys ...

  9. ios开发日期的NSDate,NSCalendar分类

    #import <Foundation/Foundation.h> @interface NSDate (XMGExtension) /** */ // @property (nonato ...

  10. css样式继承规则详解

    css样式继承规则详解 一.总结 一句话总结:继承而发生样式冲突时,最近祖先获胜(最近原则). 1.继承中哪些样式不会被继承? 多数边框类属性,比如象Padding(补白),Margin(边界),背景 ...