标签:des style http color io os java ar strong
Total Submission(s): 1412 Accepted Submission(s): 403
2 4 ATG 4 TGC -3 1 6 TGC 4 4 1 A -1 T -2 G -3 C -4
4 4 No Rabbit after 2012!Hintcase 1:we can find a rabbit whose gene string is ATGG(4), or ATGA(4) etc. case 2:we can find a rabbit whose gene string is TGCTGC(4), or TGCCCC(4) etc. case 3:any gene string whose length is 1 has a negative W.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 1000000000
using namespace std;
int n,l,w;
char s[110];
int L,rt,next[1010][26],end[1010],fail[1010],c[1010];
int dp[2][1010][1<<10];
int newnode(){
memset(next[L],0,sizeof next[L]);
c[L]=0;
end[L++]=0;
return L-1;
}
void init(){
L=0;
rt=newnode();
}
void insert(char *s,int z,int x){
int le=strlen(s),now=rt;
for(int i=0;i<le;i++){
int x=s[i]-'A';
if(!next[now][x]) next[now][x]=newnode();
now=next[now][x];
}
end[now]+=z;
c[now]|=(1<<x);
}
void build(){
queue<int> q;
int x=rt;
for(int i=0;i<26;i++){
if(next[x][i]){
fail[next[x][i]]=rt;
q.push(next[x][i]);
}
}
while(!q.empty()){
x=q.front();
q.pop();
for(int i=0;i<26;i++){
if(!next[x][i]){
next[x][i]=next[fail[x]][i];
}else{
fail[next[x][i]]=next[fail[x]][i];
q.push(next[x][i]);
}
}
}
}
void read(){
for(int i=0;i<n;i++){
scanf("%s %d",s,&w);
insert(s,w,i);
}
}
int f[4]={0,2,6,19};
void solve(){
build();
int cur=0;
for(int j=0;j<L;j++){
for(int k=0;k<(1<<n);k++) dp[cur][j][k]=-inf;
}
dp[cur][0][0]=0;
int ans=-inf;
for(int i=1;i<=l;i++){
for(int j=0;j<L;j++){
for(int k=0;k<(1<<n);k++) dp[1-cur][j][k]=-inf;
}
for(int j=0;j<L;j++){
for(int k=0;k<(1<<n);k++){
if(dp[cur][j][k]==-inf) continue;
for(int p=0;p<4;p++){
int z=f[p],nxt=next[j][z];
int C=0,tmp=0,x=nxt;
while(x){
C|=c[x];
if((k&c[x])==0) tmp+=end[x];
x=fail[x];
}
dp[1-cur][nxt][k|C]=max(dp[1-cur][nxt][k|C],dp[cur][j][k]+tmp);
}
}
}
cur=1-cur;
if(i==l){
for(int j=0;j<L;j++){
for(int k=0;k<(1<<n);k++){
ans=max(ans,dp[cur][j][k]);
}
}
}
}
if(ans<0) puts("No Rabbit after 2012!");
else printf("%d\n",ans);
}
int main(){
while(~scanf("%d%d",&n,&l)){
init();
read();
solve();
}
return 0;
}
hdu4057 Rescue the Rabbit(AC自动机+DP)
标签:des style http color io os java ar strong
原文地址:http://blog.csdn.net/wondezy/article/details/39371635