Title Effect:
Given that DNA has a correct sequential value. Please construct a length I of DNA in this sequence to make the DNA in the correct order of magnitude great. It is considered to be a negative output crackling.
。。
IDEAS:
The construction sequence is, AC has to go on the active machine, for the maximum use of DP
Dp[i][j][k] means that it is now constructed to the length I.
At this point we put the current character on the J node. and satisfies the K state. K is a 10-bit 2 binary state compression.
Note that there is a hole in the problem that a sequence may have multiple weights.
Therefore, you cannot assign values directly. Need to use bit or.
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include < Utility> #define INF 0x3f3f3f3f#define debug puts ("Fuck") using namespace std;const char tab = 0;const int max_next = 4;i NT idx;struct trie{struct trie *fail; struct Trie *next[max_next]; int Isword; int index;}; int Rev[256];trie *que[100005],ac[100005];int Head,tail;trie *new () {trie *temp=&ac[idx]; for (int i=0;i<max_next;i++) temp->next[i]=null; temp->fail=null; temp->isword=0; temp->index=idx++; return temp;} void Insert (trie *root,char *word,int len,int Ind) {trie *t=root; for (int i=0;i<len;i++) {if (t->next[rev[word[i]]]==null) t->next[rev[word[i]]]=new (); t=t->next[rev[word[i]]]; } t->isword|= (1<< (ind-1));} void Acbuild (Trie *root) {int head=0,tail=0; Que[tail++]=root; root->fail=null; while (head<tail) {trie *temp=que[head++],*p; For (inT i=0;i<max_next;i++) {if (Temp->next[i]) {if (temp==root) temp->next[i]->fail=root; else {p=temp->fail; while (P!=null) {if (P->next[i]) {temp->next[i]->fail=p->next[ I]; Break } p=p->fail; } if (P==null) temp->next[i]->fail=root; } if (Temp->next[i]->fail->isword) Temp->next[i]->isword|=temp->next[i]->fail->isword ; que[tail++]=temp->next[i]; } else if (temp==root) temp->next[i]=root; else temp->next[i]=temp->fail->next[i]; }}}void del (Trie *root) {for (int i=0;i<max_next;i++) if (Root->next[i]) del (root->next[i]); Free (root);} Char Word[105];bool Dp[1010][1035];bool tmp[1010][1035];int val[15];void tra () {for (int i=0;i<idx;i++) {if (ac[i].fail!=null) printf ("Fail =%d", Ac[i].fail-> ; index); for (int k=0;k<max_next;k++) printf ("%d", ac[i].next[k]->index); Puts (""); }}int Solve (int len,int n) {int ans=-0x3f3f3f3f; memset (dp,false,sizeof DP); Dp[0][0]=true; for (int i=1;i<=len;i++) {for (Int. j=0;j<idx;j++) for (int k=0;k< (1<<n); k++) Tmp[j][k]=false; for (int j=0;j<idx;j++) {for (int k=0;k< (1<<n); k++) {if (Dp[j][k]) {for (int p=0;p<max_next;p++) {int q=ac[j] .next[p]->index; int st=k; if (Ac[j].next[p]->isword) st|=ac[j].next[p]->isword; Tmp[q][st]=true; }}}} for (int j=0;j<idx;j++) for (int k=0;k< (1<<n); k++) {dp[j][k]=tmp[j][k]; }} for (int j=0;j<idx;j++) {for (int k=0;k< (1<<n); k++) {if (Dp[j][k]) {int sum=0; for (int p=0;p<n;p++) {if ((1<<p) &k) sum+=val[p+1]; } ans=max (Ans,sum); }}} return ans;} int main () {rev[' A ']=0; rev[' T ']=1; rev[' C ']=2; rev[' G ']=3; int n,i; while (scanf ("%d%d", &n,&i)!=eof) {idx=0; Trie *root = New (); for (int i=1;i<=n;i++) {int key; scanf ("%s%d", Word,&key); if (strlen (word) >i) continue; Insert (Root,word,strlen (Word), i); Val[i]=key; } acbuild (root); int Ans=solve (I,N); if (ans>=0) printf ("%d\n", ans); Else puts ("No Rabbit after 2012!"); } return 0;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Zoj 3545 Rescue The Rabbit (AC own active machine +DP)