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
  • 相关阅读:
    ios英语口语800句应用源码
    乐够GO应用源码完整版
    学生信息管理系统应用ios源码iPad版
    安卓版蝌蚪播放器客户端应用源码完整版
    一个Brushes笔画应用ios源码完整版
    模仿百度卫士应用最新版源码下载
    高仿QQ的即时通讯应用带服务端软件安装
    较好的IOS新闻客户端应用源码
    中国科学报客户端应用源码
    二叉树基础操作
  • 原文地址:https://www.cnblogs.com/juruohx/p/7608348.html
Copyright © 2011-2022 走看看