Bzoj1264: [ahoi2006] genetic matching match

Source: Internet
Author: User

1264: [ahoi2006] matchtime limit: 10 sec memory limit: 162 MB
Submit: 541 solved: 347
[Submit] [Status] Match Kaka dreamed last night that he and Coco had come to another planet, where the biological DNA sequences on the planet are arranged by countless bases (there are only four types on the Earth ), what's more strange is that each base that makes up a DNA sequence appears five times in this sequence! In this way, if a DNA sequence contains n different bases, its length must be 5 N. KaKa woke up and told Coco about this strange dream, and cocoa is studying genetic matching in bioinformatics these days, so he decided to write a simple DNA matching program for the creature on this strange planet. To describe the principles of gene matching, we need to first define the concept of sub-sequence: if any base (character) is extracted from a DNA sequence (string) s ), they are still arranged in the order of S into a new string of U, which is called a subsequence of S. For two DNA sequences S1 and S2, if a sequence U is a sub-sequence of S1 and S2 at the same time, U is the public sub-sequence of S1 and S2. KaKa knows two DNA sequences S1 and S2. The maximum matching between S1 and S2 is the length of the longest common subsequences of S1 and S2. [Task] compile a program :? Read two equi-long DNA sequences from the input file ;? Calculate their maximum match ;? Print the result to the output file. In the input file, the first line has an integer N, indicating that a creature on this planet uses n different bases and then numbers them as 1... An integer of N. There are two more lines, each describing a DNA sequence: containing 5 N 1... N, and each integer appears 5 times in the corresponding sequence. The output file contains only one integer, that is, the maximum number of matches between two DNA sequences. Sample input2
1 1 2 2 1 1 2 2 2
1 2 2 2 1 1 2 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

Question:

This is really a good question!

In the most traditional LCS approach, the condition for maximum value update is s [1] [I] = s [2] [J],

Now, for every second [1] [I], we already know which elements in S [2] are the same as it, obviously, you can use the prefix of these elements plus the maximum value of 1 to update the optimal value of this vertex.

Because the previously matched items must be valid, we can match I with this element to obtain a larger answer.

The tree array plays the role of fast query with the maximum prefix value.

A clever question!

Code:

 1 #include<cstdio> 2  3 #include<cstdlib> 4  5 #include<cmath> 6  7 #include<cstring> 8  9 #include<algorithm>10 11 #include<iostream>12 13 #include<vector>14 15 #include<map>16 17 #include<set>18 19 #include<queue>20 21 #include<string>22 23 #define inf 100000000024 25 #define maxn 20000+10026 27 #define maxm 500+10028 29 #define eps 1e-1030 31 #define ll long long32 33 #define pa pair<int,int>34 35 #define for0(i,n) for(int i=0;i<=(n);i++)36 37 #define for1(i,n) for(int i=1;i<=(n);i++)38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)42 43 #define mod 100000000744 45 using namespace std;46 47 inline int read()48 49 {50 51     int x=0,f=1;char ch=getchar();52 53     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}54 55     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}56 57     return x*f;58 59 }60 int n,ans,p[maxn][6],s[5*maxn],f[5*maxn];61 void change(int x,int y)62 {63     for(;x<=5*n;x+=x&(-x))s[x]=max(s[x],y);64 }65 int ask(int x)66 {67     int t=0;68     for(;x;x-=x&(-x))t=max(t,s[x]);69     return t;70 }71 72 int main()73 74 {75 76     freopen("input.txt","r",stdin);77 78     freopen("output.txt","w",stdout);79 80     n=read();81     for1(i,5*n){int x=read();p[x][++p[x][0]]=i;}82     for1(i,5*n)83     {84         int x=read();85         for3(j,5,1)86          {87              int y=p[x][j],t=ask(y-1)+1;88              if(t>f[y])f[y]=t,change(y,t);89          }90     }91     for1(i,5*n)ans=max(ans,f[i]);92     printf("%d\n",ans);93 94     return 0;95 96 }
View code

 

Bzoj1264: [ahoi2006] genetic matching match

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.