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/Others)
Total Submission(s): 200 Accepted Submission(s): 57

On the beaming day of 60th anniversary of NJUST, as a military college which was Second Artillery Academy of Harbin Military Engineering Institute before, queue phalanx is a special landscape.
Here is a M*N rectangle, and this one can be divided into M*N squares which are of the same size. As shown in the figure below:
01--02--03--04
|| || || ||
05--06--07--08
|| || || ||
09--10--11--12
Consequently, we have (M+1)*(N+1) nodes, which are all connected to their adjacent nodes. And actual queue phalanx will go along the edges.
The ID of the first node,the one in top-left corner,is 1. And the ID increases line by line first ,and then by column in turn ,as shown in the figure above.
For every node,there are two viable paths:
(1)go downward, indicated by 'D';
(2)go right, indicated by 'R';
The current mission is that, each queue phalanx has to walk from the left-top node No.1 to the right-bottom node whose id is (M+1)*(N+1).
In order to make a more aesthetic marching, each queue phalanx has to conduct two necessary actions. Let's define the action:
An action is started from a node to go for a specified travel mode.
So, two actions must show up in the way from 1 to (M+1)*(N+1).
For example, as to a 3*2 rectangle, figure below:
01--02--03--04
|| || || ||
05--06--07--08
|| || || ||
09--10--11--12
Assume that the two actions are (1)RRD (2)DDR
As a result , there is only one way : RRDDR. Briefly, you can not find another sequence containing these two strings at the same time.
If given the N, M and two actions, can you calculate the total ways of walking from node No.1 to the right-bottom node ?
For each test cases,the first line contains two positive integers M and N(For large test cases,1<=M,N<=100, and for small ones 1<=M,N<=40). M denotes the row number and N denotes the column number.
The next two lines each contains a string which contains only 'R' and 'D'. The length of string will not exceed 100. We ensure there are no empty strings and the two strings are different.
3 2
RRD
DDR
3 2
R
D
10
太伤了。
网络赛的时候没有过这题,都写对了,就是一直WA
比赛结束后问了下世界冠军cxlove,看了一眼发现是建立AC自动机的时候,fail的end要传递下,加一句话就过了,TAT
使用AC自动机+DP。
dp[x][y][i][k] 四维DP,表示R的个数是x,D的个数是y, i是在AC自动机上的节点编号。k 是状态压缩。
/* ***********************************************
Author :kuangbin
Created Time :2013/9/21 星期六 15:57:51
File Name :2013南京网络赛\1011.cpp
************************************************ */ #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MOD = 1e9+; int dp[][][][];
int n,m;
//struct Trie
//{
int next[][],fail[],end[];
int root,L;
inline int change(char ch)
{
if(ch == 'R')return ;
else return ;
}
inline int newnode()
{
for(int i = ;i < ;i++)
next[L][i] = -;
end[L++] = ;
return L-;
}
inline void init()
{
L = ;
root = newnode();
}
inline void insert(char buf[],int id)
{
int len = strlen(buf);
int now = root;
for(int i = ;i < len;i++)
{
if(next[now][change(buf[i])] == -)
next[now][change(buf[i])] = newnode();
now = next[now][change(buf[i])];
}
end[now] |= (<<id);
}
inline void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ;i < ;i++)
if(next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while( !Q.empty() )
{
int now = Q.front();
Q.pop();
end[now] |= end[fail[now]];
for(int i = ;i < ;i++)
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
inline int solve()
{
dp[][][][] = ;
int ret = ; for(int x = ;x <= n;x++)
for(int y = ;y <= m;y++)
for(int i = ;i < L;i++) for(int k = ;k < ;k++) {
if(dp[x][y][i][k] == )continue;
if(x < n)
{
int nxt = next[i][];
dp[x+][y][nxt][k|end[nxt]] += dp[x][y][i][k];
if(dp[x+][y][nxt][k|end[nxt]] >= MOD)
dp[x+][y][nxt][k|end[nxt]] -= MOD;
}
if(y < m)
{
int nxt = next[i][];
dp[x][y+][nxt][k|end[nxt]] += dp[x][y][i][k];
if(dp[x][y+][nxt][k|end[nxt]] >= MOD)
dp[x][y+][nxt][k|end[nxt]] -= MOD;
}
}
for(int i = ;i < L;i++)
{
ret += dp[n][m][i][];
if(ret >= MOD)ret -= MOD;
} return ret; }
//};
//Trie ac;
char str[]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
for(int i = ;i < ;i++)
{
scanf("%s",str);
insert(str,i);
}
build();
for(int i = ;i <= n;i++)
for(int j = ;j <= m;j++)
for(int x = ; x < L;x++)
for(int y = ;y < ;y++)
dp[i][j][x][y] = ;
printf("%d\n",solve());
}
return ;
}
HDU 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)的更多相关文章
- HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)
Divide Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)
Count The Pairs Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online
与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...
- HDU 4759 Poker Shuffle(2013长春网络赛1001题)
Poker Shuffle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdu 4750 Count The Pairs (2013南京网络赛)
n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...
- 2019ICPC南京网络赛A题 The beautiful values of the palace(三维偏序)
2019ICPC南京网络赛A题 The beautiful values of the palace https://nanti.jisuanke.com/t/41298 Here is a squa ...
- HDU 4739 Zhuge Liang's Mines (2013杭州网络赛1002题)
Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 4763 Theme Section (2013长春网络赛1005,KMP)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4762 Cut the Cake (2013长春网络赛1004题,公式题)
Cut the Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- AngularJs -- 指令中使用子作用域
下面将要介绍的指令会以父级作用域为原型生成子作用域.这种继承的机制可以创建一个隔离层,用来将需要协同工作的方法和数据模型对象放置在一起. ng-app和ng-controller是特殊的指令,因为它们 ...
- Java笔记之java.lang.String#trim
String的trim()方法是使用频率频率很高的一个方法,直到不久前我不确定trim去除两端的空白符时对换行符是怎么处理的点进去看了下源码的实现,才发现String#trim的实现跟我想像的完全不一 ...
- plsql 用法和技巧
1.导入csv文件 2.保存登录的密码
- 联通-长春处,FDD和TDD宏站,数据业务接入时延期望值默认值应为80ms
有小坑 备注:若已经跑过V5.40.00_Alpha1_Baseline.sql或V5.30.02_Beta_TO_V5.40.00_Alpha1.sql的脚本,再次运行升级脚本修改不成功,需手动在数 ...
- 从消费者角度评估RestFul的意义
相关博文: 从消费者角度评估RestFul的意义 SpringBoot 构建RestFul API 含单元测试 REST是目前业界相当火热的术语,似乎发布的API不带个REST前缀,你都不好意思和别人 ...
- Uploadify3.2中文提示
版本:Uploadify Version 3.2官网:http://www.uploadify.com Uploadify是一款基于Jquery的上传插件,用起来很方便.但上传过程中的提示语言为英文, ...
- 如何在Axure中使用FontAwesome字体图标
Font Awesome为您提供可缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. FontAwesome应用在web网页开发中非常方便, ...
- tortoise svn 忽略bin、obj等文件夹
项目空白处右击 =>TortoiseSVN => Properties => New => Other => svn:global-ignores value => ...
- mybatis-config.xml 模板
ssm模板 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration ...
- python2.x下pip install mysql-python报错解决办法
在https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python 下载该驱动网盘链接:https://pan.baidu.com/s/1r0fNYnU ...