Codeforces 463D Gargari and Permutations

http://codeforces.com/problemset/problem/463/D

题意:给出k个排列,问这k个排列的最长公共子序列的长度。

思路:只考虑其中一个的dp:f[i]=max(f[j]+1),其中i这个位置的元素在其他排列里面的位置比j这个位置的元素在其他排列里面位置要靠前。

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<iostream>
 6 int n,m,a[6][1005],pos[6][1005],f[1005];
 7 int read(){
 8     int t=0,f=1;char ch=getchar();
 9     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
10     while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();}
11     return t*f;
12 }
13 int main(){
14     n=read();m=read();
15     for (int i=1;i<=m;i++)
16      for (int j=1;j<=n;j++)
17       a[i][j]=read();
18     for (int i=2;i<=m;i++)  
19      for (int j=1;j<=n;j++)
20       pos[i][a[i][j]]=j;
21     for (int i=1;i<=n;i++){
22      f[i]=1;
23      for (int j=1;j<i;j++)
24       if (f[j]+1>f[i]){
25         bool flag=1;
26         for (int k=2;k<=m;k++)
27          if (pos[k][a[1][i]]<pos[k][a[1][j]]) flag=0;
28         if (flag) f[i]=f[j]+1; 
29       }
30     }
31     int ans=0;
32     for (int i=1;i<=n;i++)
33      ans=std::max(f[i],ans);
34     printf("%d
",ans); 
35     return 0;
36 }
原文地址:https://www.cnblogs.com/qzqzgfy/p/5623420.html