HDU4057 Rescue the Rabbit (AC automaton + pressure DP)

Source: Internet
Author: User

The topic is probably to give a few DNA fragments and their respective weights, if a DNA contains a fragment then its value is added to the weight of the fragment, and contains multiple identical DNA fragments are added once, ask the length of the maximum value of the DNA of L.

Similar to HDU2825.

    • Dp[i][j][s] represents the maximum value of the length I (Automaton transfer I step), the suffix status of the Automaton J node, the containing DNA fragment for the set S DNA
    • Dp[0][0][0]=0
    • I transfer for everyone, from Dp[i][j][s] to ATCG Four directions update dp[i+1][j '][s ']
    1. Note that you want to use a scrolling array, or you want to open a hundred-megabyte array.
    2. Use scrolling arrays to be aware of initialization.
    3. Transfer process Some calculations can be preprocessed, there are arrays, although not pretreatment should also not tle, the topic time to 10000ms.
1#include <cstdio>2#include <cstring>3#include <queue>4 using namespacestd;5 #defineINF (1&LT;&LT;30)6 inttn,ch[1111][4],fail[1111],flag[1111];7 intidx[ -];8 voidInsertChar*s,intk) {9     intx=0;Ten      for(intI=0; T[n]; ++i) { One         inty=Idx[s[i]]; A         if(ch[x][y]==0) ch[x][y]=++tn; -x=Ch[x][y]; -     } theflag[x]|=1<<K; - } - voidinit () { -memset (fail,0,sizeof(fail)); +queue<int>que; -      for(intI=0; i<4; ++i) { +         if(ch[0][i]) Que.push (ch[0][i]); A     } at      while(!Que.empty ()) { -         intx=Que.front (); Que.pop (); -          for(intI=0; i<4; ++i) { -             if(Ch[x][i]) Que.push (Ch[x][i]), fail[ch[x][i]]=Ch[fail[x]][i]; -             Elsech[x][i]=Ch[fail[x]][i]; -flag[ch[x][i]]|=Flag[ch[fail[x]][i]]; in         } -     } to } + intd[2][1111][1<<Ten],val[1<<Ten]; - intMain () { theidx['A']=0; idx['G']=1; idx['T']=2; idx['C']=3; *     intM,n,a; $     Charstr[111];Panax Notoginseng     intval[ One]; -      while(~SCANF ("%d%d",&m,&N)) { thetn=0; +memset (CH,0,sizeof(CH)); Amemset (Flag,0,sizeof(flag)); the          for(intI=0; i<m; ++i) { +scanf"%s%d", str,val+i); -             if(strlen (str) > -)Continue; $ Insert (str,i); $         } -          for(intI=1; i< (1&LT;&LT;M); ++i) { -              for(intj=0; j<m; ++j) { the                 if((I&GT;&GT;J) &1) val[i]=val[i^ (1&LT;&LT;J)]+Val[j]; -             }Wuyi         } the init (); -          for(intj=0; j<=tn; ++j) { Wu              for(intk=0; k< (1&LT;&LT;M); ++K) d[0][j][k]=-INF; -         } Aboutd[0][0][0]=0; $          for(intI=0; i<n; ++i) { -             intx=i&1; -              for(intj=0; j<=tn; ++j) { -                  for(intk=0; k< (1&LT;&LT;M); ++K) d[x^1][j][k]=-INF; A             } +              for(intj=0; j<=tn; ++j) { the                  for(intk=0; k< (1&LT;&LT;M); ++k) { -                     if(D[x][j][k]==-inf)Continue; $                      for(inty=0; y<4; ++y) { thed[x^1][ch[j][y]][k|flag[ch[j][y]]]=max (d[x^1][ch[j][y]][k|flag[ch[j][y]]],d[x][j][k]+val[k^ (k|Flag[ch[j][y]]); the                     } the                 } the             } -         } in         intres=-INF; the          for(intI=0; i<=tn; ++i) { the              for(intj=0; j< (1&LT;&LT;M); ++J) Res=max (res,d[n&1][i][j]); About         } the         if(res<0) puts ("No Rabbit after 2012!"); the         Elseprintf"%d\n", res); the     } +     return 0; -}

HDU4057 Rescue the Rabbit (AC automaton + pressure 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.