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 ']
- Note that you want to use a scrolling array, or you want to open a hundred-megabyte array.
- Use scrolling arrays to be aware of initialization.
- 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<<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<<M); ++i) { - for(intj=0; j<m; ++j) { the if((I>>J) &1) val[i]=val[i^ (1<<J)]+Val[j]; - }Wuyi } the init (); - for(intj=0; j<=tn; ++j) { Wu for(intk=0; k< (1<<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<<M); ++K) d[x^1][j][k]=-INF; A } + for(intj=0; j<=tn; ++j) { the for(intk=0; k< (1<<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<<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)