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

Problem Description

  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 ?

 
Input
  The first line contains a number T,(T is about 100, including 90 small test cases and 10 large ones) denoting the number of the test cases.
  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.
 
Output
  For each test cases,print the answer MOD 1000000007 in one line.
 
Sample Input
2
3 2
RRD
DDR
3 2
R
D
 
Sample Output
1
10
 
Source
 
Recommend
liuyiding
 

太伤了。

网络赛的时候没有过这题,都写对了,就是一直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)的更多相关文章

  1. HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

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

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

  4. HDU 4759 Poker Shuffle(2013长春网络赛1001题)

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. hdu 4750 Count The Pairs (2013南京网络赛)

    n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...

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

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

  8. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

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

随机推荐

  1. SQLSTATE[42000]

    SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT() ...

  2. django错误笔记——1242 Subquery returns more than 1 row

    在数据库查询操作过程中,子查询结果不唯一,导致外面的查询无法进行. 我的错误语句: rid = models.User.objects.filter(username=username).values ...

  3. PHP5.6 和PHP7.0区别

    1. PHP7.0 比PHP5.6性能提升了两倍. 2.PHP7.0全面一致支持64位. 3.PHP7.0之前出现的致命错误,都改成了抛出异常. 4.增加了空结合操作符(??).效果相当于三元运算符. ...

  4. 关于USBHID协议以及鼠标键盘描述符的解释【转】

    转自:https://blog.csdn.net/jiujiujiuqiuqiuqiu/article/details/47277685 一.HID设备识别 前面有提到关于SCSI协议的USB设备实现 ...

  5. VELT-0.1.5开发:使用kgdb调试Linux内核【转】

    转自:http://demo.netfoucs.com/lights_joy/article/details/44106589 VELT的全称是Visual EmbedLinuxTools,它是一个与 ...

  6. 大数据系列之数据仓库Hive安装

    Hive系列博文,持续更新~~~ 大数据系列之数据仓库Hive原理 大数据系列之数据仓库Hive安装 大数据系列之数据仓库Hive中分区Partition如何使用 大数据系列之数据仓库Hive命令使用 ...

  7. springcloud常见问题处理

    无法在任何已知服务器上执行请求. 原因是注册中心未启动.在使用springcloud搭建微服务时,一定要先启动注册中心,再启动服务端和客户端.具体报错如下: com.netflix.discovery ...

  8. 升级Chrome后无法打开网页

    最近升级了网站,发现很多普通网站Chrome 都打不开了....  IE  可以正常打开,很是郁闷,重启电脑都不行. chrome://net-internals/#dns 点击如下按钮 清楚DNS缓 ...

  9. python 双层函数调用顺序

    读大神代码,见到大神封装的接口很多都是采用双层函数形式. def 外层函数(外层参数) def 内层函数(内层参数) 函数体 return 值 return 内层函数 类似这样的形式,使用 外层函数( ...

  10. Android 拍摄(横\竖屏)视频的懒人之路

    想一想,我们聊过AudioReord,AudioTrack,MediaPlayer,那多媒体四大金刚,就剩下了MediaRecorder了(SoundPool?我这里信号不好···).其实MediaR ...