hdu 4057--Rescue the Rabbit(AC自动机+状压DP)
2018-06-17 21:45:55来源:未知 阅读 ()
题目链接
A rabbit's genes can be expressed as a string whose length is l (1 ≤ l ≤ 100) containing only 'A', 'G', 'T', 'C'. There is no doubt that Dr. X had a in-depth research on the rabbits' genes. He found that if a rabbit gene contained a particular gene segment, we could consider it as a good rabbit, or sometimes a bad rabbit. And we use a value W to measure this index.
We can make a example, if a rabbit has gene segment "ATG", its W would plus 4; and if has gene segment "TGC", its W plus -3. So if a rabbit's gene string is "ATGC", its W is 1 due to ATGC contains both "ATG"(+4) and "TGC"(-3). And if another rabbit's gene string is "ATGATG", its W is 4 due to one gene segment can be calculate only once.
Because there are enough rabbits on Earth before 2012, so we can assume we can get any genes with different structure. Now Dr. X want to find a rabbit whose gene has highest W value. There are so many different genes with length l, and Dr. X is not good at programming, can you help him to figure out the W value of the best rabbit.
The next n lines each line contains a string DNAi and an integer wi (|wi| ≤ 100), indicating this gene segment and the value it can contribute to a rabbit's W.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <string> using namespace std; const int N=1005; int a[20],tot; struct Node { Node *fail; Node *son[4]; int flag; int id; }node[N],*root; queue<Node*>Q; bool dp[2][N][1050]; int f(char c) { if(c=='A') return 0; if(c=='G') return 1; if(c=='T') return 2; if(c=='C') return 3; } void insert(string s,int id) { Node *now=root; for(int i=0;i<s.length();i++) { int x=f(s[i]); if(now->son[x]==NULL) now->son[x]=&node[tot++]; now=now->son[x]; } now->flag|=(1<<(id-1)); } void build() { Q.push(root); while(!Q.empty()) { Node *now=Q.front(); Q.pop(); for(int i=0;i<4;i++) { if(now->son[i]) { Node *p=now->fail; while(p&&(!(p->son[i]))) p=p->fail; now->son[i]->fail=(p)?(p->son[i]):root; now->son[i]->flag|=now->son[i]->fail->flag; Q.push(now->son[i]); } else now->son[i]=(now!=root)?now->fail->son[i]:root; } } } void init() { tot=1; root=&node[0]; memset(dp,0,sizeof(dp)); while(!Q.empty()) Q.pop(); for(int i=0;i<N;i++) { node[i].fail=NULL; node[i].flag=0; node[i].id=i; for(int j=0;j<4;j++) node[i].son[j]=NULL; } } int main() { int n,l; while(scanf("%d%d",&n,&l)!=EOF) { init(); for(int i=1;i<=n;i++) { string s; cin>>s; insert(s,i); scanf("%d",&a[i]); } build(); dp[0][0][0]=1; int cn=0; for(int i=0;i<l;i++) { cn^=1; memset(dp[cn],0,sizeof(dp[cn])); for(int j=0;j<tot;j++) { for(int s=0;s<(1<<n);s++) { if(!dp[cn^1][j][s]) continue; for(int k=0;k<4;k++) { int x=node[j].son[k]->id; int f=node[j].son[k]->flag; dp[cn][x][s|f]=1; } } } } int ans=-99999999; for(int i=0;i<tot;i++) { for(int s=0;s<(1<<n);s++) { if(!dp[cn][i][s]) continue; int tmp=0; int x=s; for(int k=1;k<=n;k++) { if(x&1) tmp+=a[k]; x>>=1; } ans=max(ans,tmp); } } if(ans<0) puts("No Rabbit after 2012!"); else printf("%d\n",ans); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:浅谈树状数组
下一篇:文件输入输出(一):重定向
- HDU-2955-Robberies(0-1背包) 2020-03-30
- SGU 132 Another Chocolate Maniac 2020-03-06
- hdu1455 拼木棍(经典dfs) 2020-02-29
- 协程的原理(Coroutine Theory) 2020-02-23
- 二叉堆(2)LeftistHeap 2020-02-19
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash