HDU 4758 Walk Through Squares(AC自动机+DP)
难得出一个AC自动机,我还没做到这个题呢。。。这题思路不难想,小小的状压出一维来,不过,D和R,让我wa死了,AC自动机,还得刷啊。。。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define MOD 1000000007
int dp[][][][];
int trie[][];
int o[];
int fail[],que[],p[][];
int t;
void CL()
{
t = ;
memset(trie,-,sizeof(trie));
memset(o,,sizeof(o));
memset(p,,sizeof(p));
}
void insert(char *s,int x)
{
int i,root,len,temp;
len = strlen(s);
root = ;
for(i = ; i < len; i ++)
{
temp = s[i] == 'D' ? :;
if(trie[root][temp] == -)
trie[root][temp] = t ++;
root = trie[root][temp];
}
o[root] = <<x;
}
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++];
o[front] |= o[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 cas,i,j,k,u,n,m,temp;
char str[];
scanf("%d",&cas);
while(cas--)
{
CL();
scanf("%d%d",&n,&m);
for(i = ; i < ; i ++)
{
scanf("%s",str);
insert(str,i);
}
for(i = ; i <= n; i ++)
{
for(j = ; j <= m; j ++)
{
for(k = ; k < t; k ++)
{
for(u = ; u < ; u ++)
dp[i][j][k][u] = ;
}
}
}
build_ac();
dp[][][][] = ;
for(i = ; i <= n; i ++)
{
for(j = ; j <= m; j ++)
{
for(k = ; k < t; k ++)
{
for(u = ; u < ; u ++)
{
if(i != n)
{
temp = trie[k][];
dp[i+][j][temp][u|o[temp]] = (dp[i+][j][temp][u|o[temp]] + dp[i][j][k][u])%MOD;
}
if(j != m)
{
temp = trie[k][];
dp[i][j+][temp][u|o[temp]] = (dp[i][j+][temp][u|o[temp]] + dp[i][j][k][u])%MOD;
}
}
}
}
}
int ans = ;
for(i = ; i < t; i ++)
{
ans = (ans + dp[n][m][i][])%MOD;
}
printf("%d\n",ans);
}
return ;
}
HDU 4758 Walk Through Squares(AC自动机+DP)的更多相关文章
- HDU 4758 Walk Through Squares( AC自动机 + 状态压缩DP )
题意:给你两个串A,B, 问一个串长为M+N且包含A和B且恰好包含M个R的字符串有多少种组合方式,所有字符串中均只含有字符L和R. dp[i][j][k][S]表示串长为i,有j个R,在自动机中的状态 ...
- HDU4758 Walk Through Squares AC自动机&&dp
这道题当时做的时候觉得是数论题,包含两个01串什么的,但是算重复的时候又很蛋疼,赛后听说是字符串,然后就觉得很有可能.昨天队友问到这一题,在学了AC自动机之后就觉得简单了许多.那个时候不懂AC自动机, ...
- HDU - 4758 Walk Through Squares (AC自己主动机+DP)
Description On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...
- 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 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)
Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Oth ...
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online
与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...
- hdu 4758 Walk Through Squares
AC自动机+DP.想了很久都没想出来...据说是一道很模板的自动机dp...原来自动机还可以这么跑啊...我们先用两个字符串建自动机,然后就是建一个满足能够从左上角到右下角的新串,这样我们直接从自动机 ...
随机推荐
- PHP define()的用法
define()函数理解1(着重于作用的理解) define() 函数定义一个常量. 常量的特点: 常量类似变量,不同之处在于:在设定以后,常量的值无法更改常量名,不需要开头的美元符号 ($),作用域 ...
- git push 使用总结
git push命令用于将本地分支的更新,推送到远程主机.它的格式与git pull命令相仿. $ git push <远程主机名> <本地分支名>:<远程分支名> ...
- Async/Await 最佳实践
其实好久以前就看过这个文章,以及类似的很多篇文章.最近在和一个新同事的交流中发现原来对async的死锁理解不是很透彻,正好最近时间比较充裕就再当一回搬运工. 本文假定你对.NET Framework ...
- js判断当前的访问是手机还是电脑
<script type="text/javascript"> //平台.设备和操作系统 var system ={ win : false, mac : false, ...
- PHP判断当前访问的是 微信、iphone、android 浏览器
<?phpvar ua = navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i)=="micromess ...
- 利用IdentityServer3在ASP.NET 5和Angular中实现OAuth2 Implicit Flow
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:之前介绍过的IdentityServer3虽然是基于Katana开发的,不过同样可以托 ...
- linux 读写锁应用实例
转自:http://blog.csdn.net/dsg333/article/details/22113489 /*使用读写锁实现四个线程读写一段程序的实例,共创建了四个新的线程,其中两个线程用来读取 ...
- PMP 第九章 项目人力资源管理
1制定人力资源计划 2组建项目团队 3建设项目团队 4管理项目团队 1.规划人力资源管理的作用是什么?组织图和职位描述的表现形式有哪些?RAM和RACI的关系是什么?人力资源管理计划的内容有哪些? 人 ...
- loj 1038(dp求期望)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25915 题意:求一个数不断地除以他的因子,直到变成1的时候 除的次 ...
- 汇编学习(四)——算术运算程序
(一)跳转指令 一.无条件跳转指令(不管标志寄存器,执行到这句直接跳转) 1.段内直接跳转指令 (1)指令格式: JMP SHORT short_label; IP<--IP+DB,即代码直接跳 ...