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. waven 常用构建命令

    常用命令 mvn compile : 编译maven项目 mvn test : 运行项目测试用例 mvn package : 将项目打成jar包 mvn clean : 删除target目录下生成的文 ...

  2. SSH 公钥登录

    一般使用SSH进行远程登录时需要提供密码,这也是我们所熟知的一种方式. 另外,就是通过公钥登录的方式,本文将简要介绍公钥登录的两种方法,建议使用方法二.本文也将简单演示公钥登录过程,以及强制使用公钥和 ...

  3. 一主多从+Binlog Server,主库故障无法访问,如何在从库中选举一个新主库

    一.基本环境 VMware10.0+CentOS6.9+MySQL5.7.19 ROLE HOSTNAME BASEDIR DATADIR IP PORT M ZST1 /usr/local/mysq ...

  4. MySQL Dual-Master 双向同步

    本文介绍的Mysql Dual-Master 复制实施方法可能不是最完美.最强大的.但是在我的应用环境下能很好的满足各项需求. 本文基于我们仅仅使用两台MySQL服务器的情况下,但是你会发现文章中介绍 ...

  5. Nagios配置文件nagios.cfg详解

    这里开始要讲一些Nagios的配置. 首先要看看目前Nagios的主配置路径下有哪些文件.[root@nagios etc]# ll总用量 152-rwxrwxr-x. 1 nagios nagios ...

  6. USB的挂起和唤醒(Suspend and Resume)【转】

    转自:http://m.blog.csdn.net/blog/luckywang1103/25244091 USB协议的第9章讲到USB可见设备状态[Universal Serial Bus Spec ...

  7. asp.net后台获取前台页面大小

    前台代码如下:<input type="hidden" runat="server" value="0" id="txBod ...

  8. js各种小知识

    1.获取函数里面的参数个数 function test(x,y,z){} // 获取test参数的个数 console.log(test.length)

  9. [转] caffe数据层参数说明

    原文地址:http://www.cnblogs.com/denny402/p/5070928.html 稍有修改: 数据层是每个模型的最底层,是模型的入口,不仅提供数据的输入,也提供数据从Blobs转 ...

  10. oracle中循环读出一个表的信息插入到另外一个表中

    declare cursor TAGENTMENUd is select * from TAGENTMENU where 1=1; -- 获取游标begin --遍历查询出的表 (注意 tn是整条记录 ...