Codeforces 463D Gargari and Permutations(求k个序列的LCS)

题目链接:http://codeforces.com/problemset/problem/463/D

题目大意:
给你k个序列(2=<k<=5),每个序列的长度为n(1<=n<=1000),每个序列中的数字分别为1~n,求着k个序列的最长公共子序列是多长?
解题思路:
由于每个序列的数字分别为1~n即各不相同,所以可以用pos[i][j]记录第i个序列中j的位置。
设dp[i]表示以i结尾的最长公共子序列长度,那么我们可以按顺序遍历第一个序列的位置i,
再在第一个序列中枚举位置j(j<i),然后遍历其他序列,如果对于每个序列k都满足pos[k][a[1][i]]>pos[k][a[1][j]],
那么说明a[1][i]可以接在a[1][j]后面,dp[a[1][i]]=max(dp[a[1][i],dp[a[1][j]]+1)。
这里说明一下:按顺序遍历是为了保证dp[a[1][j]]是已经求好了的,如果直接按值来遍历则会出现前面的dp值未求好的情况。

代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<string>
 6 #include<string.h>
 7 #include<cctype>
 8 #include<math.h>
 9 #include<stdlib.h>
10 #include<stack>
11 #include<queue>
12 #include<set>
13 #include<map>
14 #define lc(a) (a<<1)
15 #define rc(a) (a<<1|1)
16 #define MID(a,b) ((a+b)>>1)
17 #define fin(name)  freopen(name,"r",stdin)
18 #define fout(name) freopen(name,"w",stdout)
19 #define clr(arr,val) memset(arr,val,sizeof(arr))
20 #define _for(i,start,end) for(int i=start;i<=end;i++)  
21 #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
22 using namespace std;
23 typedef long long LL;
24 const int N=2e3+5;
25 const LL INF64=1e18;
26 const int INF=0x3f3f3f3f;
27 const double eps=1e-10;
28 
29 int dp[N],a[10][N],pos[10][N];//dp[i]表示以i结尾的最长公共子序列长度 
30 
31 int main(){
32     FAST_IO;
33     int n,q;
34     cin>>n>>q;
35     for(int i=1;i<=q;i++){
36         for(int j=1;j<=n;j++){
37             cin>>a[i][j];
38             pos[i][a[i][j]]=j;
39         }
40     }
41     
42     for(int i=1;i<=n;i++){
43         dp[a[1][i]]=1;
44         for(int j=1;j<i;j++){
45             int t1=a[1][i],t2=a[1][j];
46             bool flag=true;
47             for(int k=2;k<=q;k++){
48                 if(pos[k][t1]<=pos[k][t2]){
49                     flag=false;
50                     break;
51                 }
52             }
53             if(flag)
54                 dp[t1]=max(dp[t1],dp[t2]+1);
55         }
56     }
57     
58     int ans=1;
59     for(int i=1;i<=n;i++){
60         ans=max(ans,dp[i]);
61     }
62     cout<<ans<<endl;
63     return 0;
64 }
原文地址:https://www.cnblogs.com/fu3638/p/9131480.html