zoukankan      html  css  js  c++  java
  • D. Gargari and Permutations

    D. Gargari and Permutations

    time limit per test

    2seconds

    memory limit per test

    256 megabytes
    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.

     

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

    Solution

    题意:现在给你K(2<=K<=5)个串,每个串都是1˜N 这N个数的全排列之一, 现在要你求出这K个串的最长公共子序列的长度

    题解:假设我们已经找到了这个最长的公共子序列,我们开始分析这个公共子序列的性质.

       若x,y都是这个子序列中的元素,且x的位置在y的位置之前,那么我们可以知道,x,y 在给出的K个串中的位置也一定会满足id(x) < id(y) . 

      所以就可以大力DP了....... 

      F[i] = max(F[j] + 1 , F[i]) (id(j) <= id(i)) ...

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1000;
     4 int a[9][maxn + 99],id[9][maxn + 99],n,k,f[maxn + 99];
     5 int vis[maxn + 99];
     6 inline bool check(int x,int y){
     7     for (int i = 2 ; i <= k ; ++i) if (id[i][x] > id[i][y]) return false;
     8     return true;
     9 }
    10 int main(){
    11     scanf("%d%d",&n,&k);
    12     for (int i = 1 ; i <= k ; ++i){
    13         for (int j = 1 ; j <= n ; ++j) scanf("%d",&a[i][j]);
    14     }
    15     for (int i = 1 ; i <= k ; ++i)
    16     for (int j = 1 ; j <= n ; ++j) id[i][a[i][j]] = j;
    17 
    18     for (int i = 1 ; i <= n ; ++i) f[i] = 1;
    19     for (int i = 1 ; i <= n ; ++i){
    20        for (int j = i + 1 ; j <= n ; ++j)
    21        if (check(a[1][i],a[1][j])) f[j] = max(f[j],f[i] + 1);
    22     }
    23     int ans = 0;
    24     for (int i = 1 ; i <= n ; ++i) if (f[i] > ans) ans = f[i];
    25     cout<<ans<<endl;
    26     return 0;
    27 }
    View Code
  • 相关阅读:
    C# 按笔画排序
    WEB EXCEL OWC开发(老资料)
    JS操作Cookie
    汉字转拼音缩写取首字母
    javaScript通用数据类型校验
    嵌套Repeater 子层获得父层字段值 经典!!!
    不同于其他的provider: SQL 网络接口, error: 26 定位指定的服务器/实例时出错
    C# UDP 发送 接收
    Js 过滤空格
    高亮文本框
  • 原文地址:https://www.cnblogs.com/juruohx/p/7608348.html
Copyright © 2011-2022 走看看