HDU 4057 Rescue the Rabbit(AC自动机+DP)
一个数组开小了一点点,一直提示wa,郁闷,这题比上个题简单一点。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define N 1001
#define INF 1000000
int trie[N][];
int que[N];
int o[<<];
int flag[N];
int fail[N];
int dp1[N][<<];
int dp2[N][<<];
int t,n;
int lowbit(int t)
{
return t&(-t);
}
void CL()
{
t = ;
memset(trie,-,sizeof(trie));
memset(o,,sizeof(o));
memset(flag,,sizeof(flag));
}
int judge(char s)
{
switch(s)
{
case'A':
return ;
case'C':
return ;
case'G':
return ;
case'T':
return ;
}
return ;
}
void insert(char *str,int x,int j)
{
int i,len,root;
root = ;
len = strlen(str);
for(i = ; i < len; i ++)
{
if(trie[root][judge(str[i])] == -)
trie[root][judge(str[i])] = t ++;
root = trie[root][judge(str[i])];
}
flag[root] = <<j;
}
void build_ac()
{
int head,tail,front,i;
head = tail = ;
for(i = ; i < ; i ++)
{
if(trie[][i] != -)
{
fail[trie[][i]] = ;
que[tail++] = trie[][i];
}
else
{
trie[][i] = ;
}
}
while(head != tail)
{
front = que[head++];
flag[front] |= flag[fail[front]];
for(i = ; i < ; i ++)
{
if(trie[front][i] != -)
{
que[tail++] = trie[front][i];
fail[trie[front][i]] = trie[fail[front]][i];
}
else
{
trie[front][i] = trie[fail[front]][i];
}
}
}
}
int main()
{
int m,i,j,u,x,len,k;
int sc[];
char str[];
while(scanf("%d%d",&n,&m)!=EOF)
{
CL();
for(i = ; i < n; i ++)
{
scanf("%s%d",str,&x);
len = strlen(str);
if(len > m)
{
i --;
n -- ;
continue;
}
sc[i] = x;
insert(str,x,i);
}
for(i = ;i < n;i ++)
o[<<i] = sc[i];
for(i = ; i < <<n;i ++)
{
o[i] = o[i-lowbit(i)] + o[lowbit(i)];
}
build_ac();
for(j = ; j < t; j ++)
{
for(k = ; k < (<<n); k ++)
{
dp1[j][k] = ;
dp2[j][k] = ;
}
}
dp1[][] = ;
for(i = ; i < m; i ++)
{
for(j = ; j < t; j ++)
{
for(k = ;k < (<<n);k ++)
{
if(dp1[j][k] == ) continue;
int temp;
for(u = ;u < ;u ++)
{
temp = k|flag[trie[j][u]];
dp2[trie[j][u]][temp] += dp1[j][k];
}
}
}
for(j = ; j < t; j ++)
{
for(k = ; k < (<<n); k ++)
{
dp1[j][k] = dp2[j][k];
dp2[j][k] = ;
}
}
}
int ans = -INF;
for(j = ;j < t;j ++)
{
for(k = ;k <(<<n);k ++)
{
if(dp1[j][k])
ans = max(ans,o[k]);
}
}
if(ans >= )
printf("%d\n",ans);
else
printf("No Rabbit after 2012!\n");
}
return ;
}
HDU 4057 Rescue the Rabbit(AC自动机+DP)的更多相关文章
- HDU 4057 Rescue the Rabbit ( AC自动机 + 状态压缩DP )
模板来自notonlysuccess. 模式串只有10个,并且重复出现的分值不累加,因此很容易想到状态压缩. 将模式串加入AC自动机,最多有10*100个状态. dp[i][j][k]:串长为i,在T ...
- HDU 3341 Lost's revenge AC自动机+dp
Lost's revenge Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4057 Rescue the Rabbit
题意 给出n(n<=10)个串,每个串有个权值,然后让你构造一个长度为l(l<=100)的串,如果他包含给出的串就得到相应的权值,求可能得到的最大权值 解法 AC自动机+DP,很显然要建立 ...
- HDU 4758 Walk Through Squares(AC自动机+DP)
题目链接 难得出一个AC自动机,我还没做到这个题呢...这题思路不难想,小小的状压出一维来,不过,D和R,让我wa死了,AC自动机,还得刷啊... #include<iostream> # ...
- HDU 2825 Wireless Password【AC自动机+DP】
给m个单词,由这m个单词组成的一个新单词(两个单词可以重叠包含)长度为n,且新单词中包含的基本单词数目不少于k个.问这样的新单词共有多少个? m很小,用二进制表示新单词中包含基本单词的情况. 用m个单 ...
- hdu4057Rescue the Rabbit(ac自动机+dp)
链接 当时是因为没有做出来这道题才开了自动机的专题,现在看看还是比较简单的. 因为每个病毒串只算一次,只有10个病毒串,可以状压一下哪些状态是可以达到的,最后取一个最大值. #include < ...
- HDU 6086 Rikka with String AC自动机 + DP
Rikka with String Problem Description As we know, Rikka is poor at math. Yuta is worrying about this ...
随机推荐
- What is the difference between provider network and self-service network in OpenStack?
"self-service networking" allows users to create their own virtual networks, subnets, rout ...
- Intersection of Two Arrays | & ||
Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example ...
- windows下bat批处理实现守护进程
本文转自网络,由于找不到原作者,因而无法知道出处.如果有幸让原作者看到,请联系我加上.先转载至此. 最近几天加班加疯掉了,天天晚上没法睡.开发部的一个核心程序总是会自己宕机,然后需要手工去起,而这个服 ...
- Fastreport使用经验(转)在Delphi程序中访问报表对象
Fastreport使用经验(转) 在Delphi程序中访问报表对象 最基本的方法就是frxReport1.FindObject. 然后把返回的对象强制转换成它的类型,当然,在报表中必须真的有这么个东 ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- codeforces C. Arithmetic Progression 解题报告
题目链接:http://codeforces.com/problemset/problem/382/C 题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能 ...
- github优秀开源项目大全-iOS
github优秀开源项目大全-iOS APR 25TH, 2014 前言 本文旨在搜集github上优秀的开源项目 本文搜集的项目都是用于iOS开发 本文会持续更新… 完整客户端 ioctocat g ...
- 安装memcached服务器和PHP中添加memcache拓展模块
Memcached是一个高性能的分布式内存对象缓存系统,用于动态web应用以减轻数据库的负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提拱动态 数据驱动网站的速度. memcached ...
- 比较两个目录中的文件 diff -rq
[root@bass test]# mkdir A B [root@bass test]# tree A A └── lin 0 directories, 1 file [root@bass test ...