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

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

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

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1010; int n, k;
int a[maxn][maxn], b[maxn][maxn], dp[maxn]; int check(int x, int y) {
for (int i = 2; i <= k; i++)
if (b[i][x] > b[i][y])
return 0;
return 1;
} int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= k; i++)
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
b[i][a[i][j]] = j;
} for (int i = 1; i <= n; i++)
dp[i] = 1; int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i+1; j <= n; j++) {
if (check(a[1][i], a[1][j]))
dp[j] = max(dp[i]+1, dp[j]);
}
} for (int i = 1; i <= n; i++)
ans = max(ans, dp[i]);
printf("%d\n", ans);
return 0;
}

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1010; int num[10][maxn], vis[maxn];
int n, k;
vector<int> g[maxn]; int check(int x, int y) {
for (int i = 0; i < k; i++)
if (num[i][x] >= num[i][y])
return 0;
return 1;
} int dfs(int x) {
int ans = 0;
if (vis[x])
return vis[x]; int size = g[x].size();
for (int i = 0; i < size; i++)
ans = max(ans, dfs(g[x][i])); return vis[x] = ans + 1;
} int main() {
scanf("%d%d", &n, &k);
memset(vis, 0, sizeof(vis));
for (int i = 0; i <= n; i++)
g[i].clear(); int a;
for (int i = 0; i < k; i++)
for (int j = 1; j <= n; j++) {
scanf("%d", &a);
num[i][a] = j;
} for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (check(i, j))
g[i].push_back(j); int ans = 0;
for (int i = 1; i <= n; i++)
if (!vis[i])
ans = max(ans, dfs(i)); printf("%d\n", ans);
return 0;
}

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. jQuery异步提交form表单

    使用jquery.form.js官网现在地址表单插件来实现异步form表单提交. 先看看官方的介绍: /* Usage Note: ----------- Do not use both ajaxSu ...

  2. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  3. C語言 rand函数 进阶探讨与实现

    C语言中随机函数应用        可能大家都知道C语言中的随机函数random,但是random函数并非ANSI C标准,所以说.random函数不能在gcc,vc等编译器下编译通过. 那么怎么实现 ...

  4. js进阶ajax基本用法(创建对象,连接服务器,发送请求,获取服务器传过来的数据)

    js进阶ajax基本用法(创建对象,连接服务器,发送请求,获取服务器传过来的数据) 一.总结 1.ajax的浏览器的window对象的XMLHtmlRequest对象的两个重要方法:open(),se ...

  5. stm32的dac

  6. php xml转数组,数组转xml,array转xml,xml转array

    //数组转XML function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) ...

  7. 使用ionic3快速开发webapp(一)

    Ionic可以让我们使用web技术快速构建接近原生体验的跨平台移动应用. 一.安装ionic 1.需要先安装 Node.js(版本8.x之上): 2.安装cordova 和 ionic: $ npm ...

  8. 我眼中的c++编程总结-20150602

    断断续续的学习了非常多东西,有51.Avr.ARM.PLC.C\C++.C#.TB.MC.mql4.linux....等等,近乎填鸭或者囫囵吞枣的.甚至饿狼般的扑到里面,慢慢的积累和理解中,非常多知识 ...

  9. 【u017】请柬

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣传剧院, ...

  10. Android中密码输入内容可见性切换

    今天在做项目的时候遇到了一个关于密码输入框可见性切换问题,上网搜了搜,这里面门道还不小,做一个记录吧,下次遇到就好解决了. 首先写了一个简单的测试工程: <LinearLayout xmlns: ...