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 ...
随机推荐
- MySql的like语句中的通配符:百分号、下划线和escape
MySql的like语句中的通配符:百分号.下划线和escape %:表示任意个或多个字符.可匹配任意类型和长度的字符. Sql代码 select * from user where user ...
- Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
- maven No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
maven install项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-pl ...
- firefox30浏览器,在使用quit()方法退出时,plugin-container.exe崩溃的问题
如题,崩溃截图如下: 解决办法: 对于版本号大于29的firefox,需要在其安装目录下,删除plugin-container.exe,不然使用webdriver的quit()方法关闭浏览器时会报错. ...
- 如何分割一个utf8字符串(保证单个汉字的完整性)
std::list<std::string> split_utf8_string(const std::string& text) { std::list<std::stri ...
- swift 中delegate的使用
今天写了delegate,遇到以下问题: 这里protocol的写法有问题,如果delegate指向一个实现了某个协议对象的引用,在oc里是这样写delegate的类型 id<protocol& ...
- Java for LeetCode 040 Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【python】继承时注意事项
1. __init__ 注意事项 如果父类有__init__函数,子类没有,则子类自动调用父类__init__函数 如果父类有__init__函数,子类也有,则子类必须主动调用父类__init__函数 ...
- 手把手教你cuda5.5与VS2010的编译环境搭建
参考:http://www.cnblogs.com/xing901022/archive/2013/08/09/3248469.html 目前版本的cuda是很方便的,它的一个安装里面包括了Toolk ...
- Cocos2dx中零散知识点
cocos2dx中有三种定时器:schedule,scheduleUpdate,scheduleOnce.功能分别是 每隔几秒调用自定义函数.调用系统默认的update()函数.只调用一次自定义函数 ...