1264: [AHOI2006] gene matches match time limit:10 Sec Memory limit:162 MB
submit:904 solved:578
[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
Hometown of the province of the topic ~ very good one problem, the general LCS time Complexity is O (n^2), so the problem should be tree-like array optimization
After all, I am a konjac konjac, do not know the implementation of the tree-like array, so the problem I quoted someone else's spicy ~ marked the Source: Http://blog.csdn.net/popoqqq/article/details/39958251?utm_source=tuicool
We record 5 positions for each number in the 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 <cmath>3#include <cstring>4#include <cstdlib>5#include <queue>6#include <stack>7#include <vector>8#include <iostream>9#include"algorithm"Ten using namespacestd; OnetypedefLong LongLL; A Const intmax=20005; - intN; - intf[max*5]={0}; the intc[max*5]={0},pos[max][6]={0}; - voidUpdateintXinty) { - for(; x<=5*n;x+= (x&-x)) -c[x]=Max (c[x],y); + } - intSearchintx) { + intZ0); A for(;x>0; x-= (x&-x)) atz=Max (c[x],z); - returnZ; - } - intMain () { -Freopen ("match.in","R", stdin); -Freopen ("Match.out","W", stdout); in inti,j,k; - intAns0); toscanf"%d",&n); + for(i=1; i<=5*n;i++) -{scanf ("%d",&j); thepos[j][++pos[j][0]]=i; * } $ for(i=1; i<=5*n;i++)Panax Notoginseng{scanf ("%d",&k); - for(j=5; j>=1; j--) the{intx, y; +x=Pos[k][j]; AY=search (x1)+1; the if(f[x]<y) +{f[x]=y; - Update (x, y); $ } $ } - } - for(i=1; i<=5*n;i++) theans=Max (ans,f[i]); -printf"%d", ans);Wuyi return 0; the}
BZOJ-1264: [AHOI2006] gene matches match (tree array +DP)