Codeforces #264 (Div. 2) D. Gargari and Permutations,

Source: Internet
Author: User

Codeforces #264 (Div. 2) D. Gargari and Permutations,

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 foundKPermutations. Each of them consists of numbers1, limit 2, limit..., limit ,...,NIn some order. Now he shocould 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 integersNAndK(1 digit ≤ DigitNLimit ≤ limit 1000; 2 limit ≤ limitKLimit ≤ limit 5). Each of the nextKLines contains integers1, limit 2, limit..., limit ,...,NIn 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].

Obtain the longest common subsequence with k lengths of n.

Idea 1: store each number in its own string location. Because the result is one of the 1st strings, we enumerate the possibility of 1st strings, then, check that if the longest common subsequence ending with a [j] is true, the [I] of each string is located before a [j, then 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;}


Idea 2: If a number I is located in front of j in each string, then I to j will have a directed edge, and the question will be converted to DAG to find the longest

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




How to view test data on codeforces

Enter the competition, click my submissions, and then click the RUN number under # To view the test data.

Codeforces question: How can I attack others in codeforces?

For a question, you must first pass TEST. Then return to the PROBLEM list of the game, lock the lock behind the question (the lock won't be submitted again, so don't lock it if you're unsure), and then go to the ROOM, you can check other people's Code. There is a HACK button below. Click it and enter the example that you think is wrong.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.