bzoj1264 [AHOI2006] gene matches match tree array +lcs

Source: Internet
Author: User

1264: [AHOI2006] gene matches match time limit:10 Sec Memory limit:162 MB
submit:1255 solved:835
[Submit] [Status] [Discuss] Description gene matching (match) Kaka dreamed last night that he and cocoa had come to another planet, and that the DNA sequence of the creatures on the planet was made up of countless bases (only 4 on earth), and even more strangely, each of the bases that made up the DNA sequence appeared exactly 5 times in that sequence! So if a DNA sequence has n different bases, it must be 5N in length. Kaka woke up to tell the strange dream of cocoa, and cocoa these days are studying the genetic matching problem in bioinformatics, so he decided to write a simple DNA matching program for the creature on this strange planet. In order to describe the principle of gene matching, we need to set the concept of a sub-sequence: If you extract some bases (characters) from a DNA sequence (string) s, and they are still arranged in the order of S in a new string u, you are called a subsequence of S. For two DNA sequences S1 and S2, if there is a sequence u that is also a subsequence of S1 and S2, then U is the common subsequence of S1 and S2. Kaka is known to have two DNA sequences S1 and S2, and the maximum match for S1 and S2 is the length of the longest common sub-sequence of S1 and S2. [Task] Write a program:? Read two equal-length DNA sequences from the input file; calculate their maximum match;? Print the results you get to the output file. The first line in the input file has an integer n, indicating that some species on the planet use n different bases, and they are numbered 1 later ... An integer of N. There are two lines below, each of which describes a DNA sequence: Contains 5N of 1 ... An integer of n, and each integer occurs exactly 5 times in the corresponding sequence. There is only one integer in the output file, which is the maximum number of matches for two DNA sequences. Sample Input2
1 1 2 2 1 1 2 1 2 2
1 2 2 2 1 1 2 2 1 1
Sample Output7
HINT

[Data constraints and scoring methods]
60% of the test data: 1<=n <= 1 000
100% of the test data: 1<=n <= 20 000

Source

Given n number and two sequences of length n*5, each number happens 5 times, two sequences of LCS are obtained

n<=20000, the sequence length is 10W, the naïve O (n^2) must be timed out

So we consider some of the properties of LCS

LCS decision +1 condition is a[i]==b[j] so we record 5 positions for each number in a sequence

Sweep B[i] [for each b[i] find B[i] in the 5 positions in a 5 positions each F[pos] value can be b[i] update then find f[1] to f[pos-1] The maximum value +1 update F[pos] can

This tree-like array maintains time complexity O (NLOGN)

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cstdio>6 7 #defineM 2000078 using namespacestd;9InlineintRead ()Ten { One     intx=0, f=1;CharCh=GetChar (); A      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {x= (x<<3) + (x<<1) +ch-'0'; ch=GetChar ();} -     returnx*F; the } -  - intN,ans; - inta[m*5],b[m*5],c[m*5],f[m*5],pos[m][6]; +  - voidUpdate (intXinty) + { A      for(; x<=n*5; x+=x&-x) atc[x]=Max (c[x],y); - } - intGet_ans (intx) - { -     intRe=0; -      for(; x;x-=x&-x) inRe=Max (re,c[x]); -     returnre; to } + intMain () - { then=read (); *      for(intI=1; i<=n*5; i++) $     {Panax Notoginsenga[i]=read (); -pos[a[i]][++pos[a[i]][0]]=i; the     } +      for(intI=1; i<=n*5; i++) b[i]=read (); A      for(intI=1; i<=n*5; i++) the          for(intj=5; j;j--) +         { -             intk=Pos[b[i]][j]; $F[k]=max (F[k],get_ans (K-1)+1); $ Update (K,f[k]); -ans=Max (ans,f[k]); -         } theprintf"%d\n", ans); -}

bzoj1264 [AHOI2006] gene matches match tree array +lcs

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.