"Dp+ tree-like array" bzoj1264-[ahoi2006 "gene matches match

Source: Internet
Author: User

"The main topic"

Given the number of N and two sequences with a length of n*5, the number in the two series is 1. n composition, and 1. Each number in n occurs exactly 5 times, and an LCS of two sequences is obtained.

Ideas

Preprocess the five locations that each number appears in A[i]. F[i] shows the longest common substring at the end of A[i] (* This avoids the discussion of intersections).

Processing B[i] In turn, for each b[i] found in a[i] Five position transfer, with the NOWP, the transfer is very simple: F[nowp]=max (f[nowp],query (nowp-1) +1), where the prefix maximum value needs to be maintained.

To know that the prefix maximum value can be maintained with bit.

It is important to note, however, that it must be from 5 Downto 1. Why is it? Since the NOWP is arranged in ascending order, if from the smallest start, then the subsequent F-transfer may be calculated in the previously obtained F, but in fact they correspond to the same b[i].

So from the big to the small.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 Const intmaxn=20000+ -;8 intN;9 inta[5*maxn],b[5*maxn],e[5*maxn],f[5*MAXN];Ten //F[i] represents the longest common substring at the end of A[i], so you can avoid discussing cross - One intpos[maxn][6]; A intans=0; -  - intLowbit (intx) the { -     return(x& (-x)); - } -  + intQueryintx) - { +     intret=0;//the initial value must be set to 0 instead of-1 . A      while(x>0) Ret=max (Ret,e[x]), x-=lowbit (x); at     returnret; - } -  - voidUpdateintXintDelta) - { -      while(x<=5*n) E[x]=max (E[x],delta), x+=lowbit (x); in }  -  to voidInit () + { -Memset (E,0,sizeof(e)); theMemset (F,0,sizeof(f)); *Memset (A,0,sizeof(a)); $scanf"%d",&n);Panax Notoginseng      for(intI=1; i<=5*n;i++)  -     { thescanf"%d",&a[i]); +pos[a[i]][++pos[a[i]][0]]=i; A     } the      for(intI=1; i<=5*n;i++) scanf ("%d",&B[i]);  + } -  $ voidDP () $ { -      for(intI=1; i<=5*n;i++) -          for(intj=5; j>=1; j--)//we must move from the back . the         { -             intnowp=Pos[b[i]][j];WuyiF[nowp]=max (F[nowp],query (nowp-1)+1); the Update (NOWP,F[NOWP]); -ans=Max (ANS,F[NOWP]); Wu         } -printf"%d\n", ans); About } $  - intMain () - { - init (); A DP (); +     return 0;  the}

"Dp+ tree-like array" bzoj1264-[ahoi2006 "gene matches 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.