uva10635 LCS mapping to Lis

Source: Internet
Author: User

Topic given

2 sequence, we want to seek LCS, but the length of the sequence is 250*250, the time complexity of the LCS is O (n*n), so we can not solve the number of the first sequence, by location, map to 1, 2, 3, 4, 5, 6, 7, 8, 9 then you will get a mapping function, the second sequence , mapping to a new sequence is equivalent to using a mapping function to map two strings to two new strings we will find that the length of the LCS will not change because the same character mappings in two sequences are still the same, so only two sequences of LCS are required and then we find that the first sequence is incremented, Then the LCS must also be incremented, so we're just asking for the longest increment subsequence of the second sequence. The answer is: Yes and Lis has O (NLOGN) algorithm so the complexity is reduced so that the problem can be solved
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <algorithm>5#include <iostream>6#include <queue>7#include <stack>8#include <vector>9#include <map>Ten#include <Set> One#include <string> A using namespacestd; -typedefLong LongLL; - Const intINF =1<< -; the Const intN =90000; - intA[n],b[n]; - intReflect[n]; - intSt[n],top; + intMain () - { +     intt,n,p,q,i,j,tcase; Ascanf"%d",&tCase); at      for(t=1; t<=tcase; ++t) -     { -top =0; -scanf"%d%d%d",&n,&p,&q); -p++; -q++; in          for(i=1; i<=p; ++i) -         { toscanf"%d",&a[i]); +Reflect[a[i]] =i; -         }     the          for(i=1; i<=q; ++i) *         { $scanf"%d",&b[i]);Panax NotoginsengB[i] =Reflect[b[i]]; -         }     theSt[top] = b[1]; +          for(i=2; i<=q; ++i) A         { the             if(B[i] >St[top]) +             { -St[++top] =B[i]; $             } $             Else -             { -                 intLow =0, high =top; the                  while(Low <=High ) -                 {Wuyi                     intMid = (low + high) >>1; the                     if(B[i] >St[mid]) -Low = mid +1; Wu                     Else -High = mid-1; About                 } $St[low] =B[i]; -             }     -         } -printf"Case %d:%d\n", t,top+1); A     } +      the     return 0; -}
View Code

HDU's Magic Board also uses a mapping, which becomes pre-processing and then reads the data to output the answer immediately.

uva10635 LCS mapping to Lis

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.