HDU3341 Lost ' s Revenge (AC automaton +DP)

Source: Internet
Author: User

The title is to rearrange a DNA to contain the most number-theoretic genes.

Considering that memory is probably the only way to represent state:

Dp[i][a][c][g][t], which represents the number of arithmetical genes with the largest number of ACGT and the node with the current suffix status of Automaton I

Where ACGT can be hashed into an integer (a*c*g*t+c*g*t+g*t+t), so that the two-dimensional array on the line, and the second dimension at most 11*11*11*11.

The next shift is still me for everyone, I was thrown into a queue to update the status value with the queue.

This problem is quite card constant, had to hand-written queue, the last 4500msAC, or almost timed out, the code is also very messy.

1#include <cstdio>2#include <cstring>3 using namespacestd;4 inttn,ch[555][4],fail[555],flag[555];5 intidx[ -];6 voidInsertChar*s) {7     intx=0;8      for(intI=0; T[n]; ++i) {9         inty=Idx[s[i]];Ten         if(ch[x][y]==0) ch[x][y]=++tn; Onex=Ch[x][y]; A     } -++Flag[x]; - } the intque[555]; - voidinit () { -memset (fail,0,sizeof(fail)); -     intFront=0, rear=0; +      for(intI=0; i<4; ++i) { -         if(ch[0][i]) que[rear++]=ch[0][i]; +     } A      while(front!=rear) { at         intx=que[front++]; -          for(intI=0; i<4; ++i) { -             if(Ch[x][i]) que[rear++]=ch[x][i],fail[ch[x][i]]=ch[fail[x]][i],flag[ch[x][i]]+=Flag[ch[fail[x]][i]]; -             Elsech[x][i]=Ch[fail[x]][i]; -         } -     } in } - intd[555][14641]; to intquex[555*14641],quey[555*14641]; + intMain () { -idx['A']=0; idx['C']=1; idx['G']=2; idx['T']=3; the     Charstr[ -]; *     intN,cse=0; $      while(~SCANF ("%d", &n) &&N) {Panax Notoginsengtn=0; -memset (CH,0,sizeof(CH)); thememset (Flag,0,sizeof(flag)); +          while(n--){ Ascanf"%s", str); the Insert (str); +         } - init (); $scanf"%s", str); $         inttimes[4]={0}; -          for(intI=0; Str[i]; ++i) { -++Times[idx[str[i]]; the         } -         intTcal0= (times[1]+1) * (times[2]+1) * (times[3]+1);Wuyi         intTcal1= (times[2]+1) * (times[3]+1); the         intTcal2= (times[3]+1); -memset (d,-1,sizeof(d)); Wud[0][0]=0; -         intFront=0, rear=0, x,y,ny,cnt[4]; Aboutquex[rear]=0; quey[rear]=0; ++Rear; $          while(front!=rear) { -X=quex[front]; Y=quey[front]; ++Front; -ny=y; -cnt[0]=ny/tcal0; Any-=cnt[0]*tcal0; +cnt[1]=ny/Tcal1; theny-=cnt[1]*Tcal1; -cnt[2]=ny/Tcal2; $cnt[3]=ny-cnt[2]*Tcal2; the              for(intI=0; i<4; ++i) { the                 if(Cnt[i]>=times[i])Continue; the++Cnt[i]; theny=cnt[0]*tcal0+cnt[1]*tcal1+cnt[2]*tcal2+cnt[3]; -                 if(d[ch[x][i]][ny]==-1){ ind[ch[x][i]][ny]=d[x][y]+Flag[ch[x][i]]; theQuex[rear]=ch[x][i]; Quey[rear]=ny; ++Rear; the}Else if(d[ch[x][i]][ny]<d[x][y]+Flag[ch[x][i]]) { Aboutd[ch[x][i]][ny]=d[x][y]+Flag[ch[x][i]]; the                 } the--Cnt[i]; the             } +         } -y=times[0]*tcal0+times[1]*tcal1+times[2]*tcal2+times[3]; the         intres=0;Bayi          for(intx=0; x<=tn; ++x) { the             if(Res<d[x][y]) res=D[x][y]; the         } -printf"Case %d:%d\n",++cse,res); -     } the     return 0; the}

HDU3341 Lost ' s Revenge (AC automaton +DP)

Related Article

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.