POJ 2533 longest Ordered subsequence LCS O (N*log (n))

Source: Internet
Author: User

Topic links

The longest ascending subsequence O (N*log (n)) method can only be used to find the length of the sequence can not be obtained.

#include <iostream>#defineSIZE 1001using namespacestd;intMain () {intI, J, N, top, temp; intStack[size];  while(Cin >>N) {top=0; /*the first element may be 0*/stack[0] = -1;  for(i =0; I < n; i++) {cin>>temp; /*The stack is larger than the top element of the stack .*/        if(Temp >Stack[top]) {stack[++top] =temp; }        Else        {            intLow =1, high =top; intmid; /*the first number in a binary search stack that is larger than temp*/             while(low<High ) {Mid= (Low+high)/2; if(stack[mid]>temp) high=mid; Else Low=mid+1; }            /*Replace with temp*/Stack[low]=temp; }    }    /*the longest sequence number is the size of the stack*/cout<< Top <<Endl; }    return 0;}

This is the complexity O (n^2), which can be obtained for both length and sequence.

Ritchie, -: -: the#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=1e3+5;CharS1[MAXN],S2[MAXN];intB[MAXN][MAXN],DP[MAXN][MAXN];voidprintIintj) {    if(i==0|| j==0)return ; if(b[i][j]==1) {PR (i-1, J-1); printf ("%c", S1[i]); }    Else if(b[i][j]==2) PR (i-1, J); ElsePR (i,j-1);}intMain () { while(SCANF ("%s%s", s1,s2)! =EOF) {memset (b,0,sizeof(b)); Memset (DP,0,sizeof(DP)); intLen1=strlen (S1), len2=strlen (S2);  for(inti=len1;i>=1; i--) s1[i]=s1[i-1];  for(inti=len2;i>=1; i--) s2[i]=s2[i-1];  for(intI=1; i<=len1;i++)         for(intj=1; j<=len2;j++)        {            if(s1[i]==S2[j]) {Dp[i][j]=dp[i-1][j-1]+1; B[I][J]=1; }            Else if(dp[i-1][j]>dp[i][j-1]) {Dp[i][j]=dp[i-1][j]; B[I][J]=2; }            Else{Dp[i][j]=dp[i][j-1]; B[I][J]=3; }        }        //This is the length .//printf ("%d\n", Dp[len1][len2]); //This is the LCS sequence .PR (LEN1,LEN2); Puts (""); }    return 0;}

POJ 2533 longest Ordered subsequence LCS O (N*log (n))

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.