http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19051
The title means giving you two strings of numbers (counted as a B array), allowing you to ask for their longest common subsequence. The number length is n * N (n <= 250) so O (n^2) must not be over. So I want to find O (NLONGN).
Since the topic gives the number within 1 ~ (n^2) and the number does not repeat. So, for each number in the a array, if we all know that he has a first position in the B array (the position is a C array), we just need to find an ascending subsequence in the C array. And Lis can be made by O (NLONGN).
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intINF =999999999;inta[62505];ints[62505];intdp[62505];intMain () {intT; CIN>>T; for(intI_case =1; I_case <= T; i_case++) { intN, _a, _b; CIN>> N >> _a >>_b; Memset (A,-1,sizeofa); for(intj=0; j<=_a; J + +) { inttmp; scanf ("%d", &tmp); A[TMP]=J; } for(intI=0; i<=_b; i++) { inttmp; scanf ("%d", &tmp); S[i]=A[tmp]; } intk=0; for(intI=0; i<=_b; i++) { if(S[i] = =-1)Continue; S[k++] =S[i]; } Fill (DP, DP+K, INF); for(intI=0; I < K; i++) { *lower_bound (DP, DP+K, s[i]) =S[i]; } printf ("Case %d:%d\n", I_case, (int) (Lower_bound (DP, DP+K, INF)-DP)); } return 0;}
View Code
UVA 10635 Prince and Princess longest common sub-sequence (NLONGN)