题目链接

难得出一个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)的更多相关文章

  1. 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,在自动机中的状态 ...

  2. HDU4758 Walk Through Squares AC自动机&&dp

    这道题当时做的时候觉得是数论题,包含两个01串什么的,但是算重复的时候又很蛋疼,赛后听说是字符串,然后就觉得很有可能.昨天队友问到这一题,在学了AC自动机之后就觉得简单了许多.那个时候不懂AC自动机, ...

  3. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  4. 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 ...

  5. HDU 2457 DNA repair(AC自动机+DP)题解

    题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...

  6. 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 ...

  7. HDU 2425 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online

    与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...

  9. hdu 4758 Walk Through Squares

    AC自动机+DP.想了很久都没想出来...据说是一道很模板的自动机dp...原来自动机还可以这么跑啊...我们先用两个字符串建自动机,然后就是建一个满足能够从左上角到右下角的新串,这样我们直接从自动机 ...

随机推荐

  1. Linux环境下段错误的产生原因及调试方法小结

    转载自http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...

  2. [LeetCode] Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  3. HDU2546 饭卡(背包)

    开始写成01背包的形式,求m元可买物品价值的最大值 dp[j] = max(dp[j], dp[j - pri[i]] + pri[i]) 结果为m - dp[m] 但后来发现是有问题的, 比如这组过 ...

  4. HDU 1227 Fast Food

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1227 题意:一维坐标上有n个点,位置已知,选出k(k <= n)个点,使得所有n个点与选定的点中 ...

  5. jquery获取和设置元素高度宽度

    jquery获取和设置元素高度宽度 1.height()/ width() 取得第一个匹配元素当前计算的高度/宽度值(px) height(val)/ width(val) 为每个匹配的元素设置CSS ...

  6. 关于Application Insights遥测功能使用【遇到问题】

    简介:Application Insights是微软发布的一个在线服务,可以监测自己的网站应用,进行性能管理以及使用分析. Application Insights功能一开始是出现在Visualstu ...

  7. APP交互

    交互设计基本功!5个值得学习的APP交互方式http://www.uisdc.com/5-interactive-design-worth-learning 移动App交互设计10大趋势–你用到了吗? ...

  8. Codeforces Round #370 (Div. 2) D. Memory and Scores DP

    D. Memory and Scores   Memory and his friend Lexa are competing to get higher score in one popular c ...

  9. Linux学习笔记(15)shell基础之Bash基本功能

    1 shell概述 shell是一个命令解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序.用户可以用shell启动.挂起.停止甚至是编写一些程序. shell是一个功能强大 ...

  10. Atom 和 markdown 基本使用

    Atom 和 markdown 基本使用 Atom 常用的快捷键 Cmd + Shift + P 打开命令窗口,可以运行各种菜单功能. Cmd + T 快速的多文件切换 Ctrl + G 文件内跳转到 ...