UVA 10635 Prince and Princess longest common sub-sequence (NLONGN)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.