Question Portal: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? Id = 18201
In fact, it is not difficult, but after a long time, I found that the original question was not clearly read, attention, the original sequence was too pitfall,
I have seen that many others use the longest common subsequence, but I use the longest ascending subsequence to map the original sequence to an ascending sequence, the data for this question exactly meets this condition.
For example, if the correct sequence is $5 \ 6 \ 4 \ 1 \ 3 \ 2 $, I can map it
$ Id [5] \ rightarrow 1 $ id [6] \ rightarrow 2 $ id [4] \ rightarrow 3 $
$ Id [1] \ rightarrow 4 $ id [3] \ rightarrow 5 $ id [2] \ rightarrow 6 $
For another sequence, such as $6 \ 1 \ 3 \ 2 \ 5 \ 4 $
Then I can map it to $2 \ 4 \ 5 \ 6 \ 1 \ 3 $ to find the longest ascending subsequence of the sequence.
Directly paste the Code:
#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int maxn = 30;int ID[maxn];int b[maxn];int ans1,ans2;int a[maxn];int c[maxn];int n;int opt[maxn];int solve(){ int MAX = 0; a[0] = -100; memset(opt,0,sizeof(opt)); for(int i = 1;i <= n;++i) for(int j = 0;j <= i-1;++j){ if(a[j] <= a[i]) opt[i] = max(opt[i],opt[j] + 1); } for(int i = 1;i <= n;++i){ MAX = max(MAX,opt[i]); } return MAX;}void print(){ for(int i = 1;i <= n;++i) cout<<a[i]<<" "; cout<<endl;}int getid(int index){ for(int i = 1;i <= n;++i) if(ID[i] == index) return i;}int main(){ //freopen("in.txt","r",stdin); int fuck; while(~scanf("%d",&n)){ for(int i = 1;i <= n;++i){ scanf("%d",&fuck); ID[fuck] = i; } while(1){ ans1 = 0; for(int i = 1;i <= n;++i){ if(scanf("%d",&fuck)==EOF) goto holly_shit; a[fuck] = getid(i); } printf("%d\n",solve()); memset(a,0,sizeof(a)); } holly_shit: continue; } return 0;}
Simple DP with a vulnerability