hdu4758 Walk Through Squares (AC自己主动机+DP)
Walk Through Squares
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 944 Accepted Submission(s): 277

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.
2
3 2
RRD
DDR
3 2
R
D
1
10
5017 5016 5015
pid=5014" target="_blank">
5014
pid=5013" target="_blank">
5013
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define mod 1000000007
using namespace std;
int dp[110][110][210][4];
int m,n; int next[210][2],L,rt,end[210],fail[210];
inline int newnode(){
next[L][0]=next[L][1]=0;
end[L++]=0;
return L-1;
}
inline void init(){
L=0;
rt=newnode();
}
inline void insert(char *s,int z){
int l=strlen(s),x=rt;
for(int i=0;i<l;i++){
int z=(s[i]=='R' ? 0:1);
if(!next[x][z]) next[x][z]=newnode();
x=next[x][z];
}
end[x]=z;
}
inline void build(){
queue<int> q;
fail[0]=0;
for(int i=0;i<2;i++){
if(next[rt][i]!=0){
fail[next[rt][i]]=rt;
q.push(next[rt][i]);
}
}
while(!q.empty()){
int x=q.front();
q.pop();
end[x]|=end[fail[x]];//!!!!!
for(int i=0;i<2;i++){
if(next[x][i]==0){
next[x][i]=next[fail[x]][i];
}else{
fail[next[x][i]]=next[fail[x]][i];
q.push(next[x][i]);
}
}
}
}
char s[110];
inline void read(){
scanf("%d%d",&n,&m);
scanf("%s",s);
insert(s,1);
scanf("%s",s);
insert(s,2);
} inline void solve(){
build();
for(int i=1;i<=m+1;i++)for(int j=1;j<=n+1;j++)
for(int k=0;k<L;k++)for(int p=0;p<4;p++) dp[i][j][k][p]=0;
//memset(dp,0,sizeof dp);
dp[1][1][0][0]=1;
for(int i=1;i<=m+1;i++){
for(int j=1;j<=n+1;j++){
for(int k=0;k<L;k++){
for(int p=0;p<4;p++){
int z;
if(j>1){
z=next[k][0];
dp[i][j][z][end[z]|p]+=dp[i][j-1][k][p];
if(dp[i][j][z][end[z]|p]>mod) dp[i][j][z][end[z]|p]-=mod;
}
if(i>1){
z=next[k][1];
dp[i][j][z][end[z]|p]+=dp[i-1][j][k][p];
if(dp[i][j][z][end[z]|p]>mod) dp[i][j][z][end[z]|p]-=mod;
}
}
}
}
}
int ans=0;
for(int i=0;i<L;i++){
ans+=dp[m+1][n+1][i][3];
if(ans>mod) ans-=mod;
}
printf("%d\n",ans);
} int main(){
int t;
scanf("%d",&t);
for(int ca=1;ca<=t;ca++){
init();
read();
solve();
}
return 0;
}
/*
100 99
DRDDRD
DDRD
*/
hdu4758 Walk Through Squares (AC自己主动机+DP)的更多相关文章
- HDU - 4758 Walk Through Squares (AC自己主动机+DP)
Description On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
- HDU - 2825 Wireless Password(AC自己主动机+DP)
Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...
- Hdu 3341 Lost's revenge (ac+自己主动机dp+hash)
标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...
- poj 3691 DNA repair(AC自己主动机+dp)
DNA repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5877 Accepted: 2760 Descri ...
- hdu4057 Rescue the Rabbit(AC自己主动机+DP)
Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU4758 Walk Through Squares AC自动机&&dp
这道题当时做的时候觉得是数论题,包含两个01串什么的,但是算重复的时候又很蛋疼,赛后听说是字符串,然后就觉得很有可能.昨天队友问到这一题,在学了AC自动机之后就觉得简单了许多.那个时候不懂AC自动机, ...
- HDU - 4511 小明系列故事――女友的考验(AC自己主动机+DP)
Description 最终放寒假了,小明要和女朋友一起去看电影.这天,女朋友想给小明一个考验,在小明正准备出发的时候.女朋友告诉他.她在电影院等他,小明过来的路线必须满足给定的规则: 1.如果小明 ...
- Codeforces 86C Genetic engineering (AC自己主动机+dp)
题目大意: 要求构造一个串,使得这个串是由所给的串相连接构成,连接能够有重叠的部分. 思路分析: 首先用所给的串建立自己主动机,每一个单词节点记录当前节点可以达到的最长后缀. 開始的时候想的是dp[i ...
随机推荐
- 从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库)
原文:从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库) 1.安装对应dll Update-Package Xama ...
- POJ1163 The Triangle 【DP】
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36918 Accepted: 22117 De ...
- Effective C++规定45 附加代码
这部分是额外的代码的博客.键45条款想法已经实现. #include<iostream> using namespace std; template<typename T> c ...
- C# 引用类型与值类型的区别
//引用类型(使用了class) class SomeRef{public Int32 x;} //值类型(使用了struct) struct SomeVal{public Int32 x;} sta ...
- HDU4309-Seikimatsu Occult Tonneru(最大流)
Seikimatsu Occult Tonneru Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- Windows 8 应用开发 - 磁贴
原文:Windows 8 应用开发 - 磁贴 我们开发的应用在Win8 界面中会以磁贴形式展现,默认情况下磁贴会显示应用图标,即项目工程中的Logo.png图片文件.开发人员可按应用的需要使 ...
- XMPP我写底层协议(零)--废话和准备开幕前
当我想写一个非常早期的一点总结.但总是忙没有时间停止做这样的事情. 秦与我的兄弟之前说的,这并不是说我没开灵.但是,因为很多事情还没有时间来写blog. 我没有完全理解,真到自己在这个位置上的时间,能 ...
- javascript有用小技巧—实现分栏显示
记得给师哥师姐測试考试系统的时候,看到他们的考试页面能够实现隐藏左边的考生信息部分,当时认为好高大上.好人性化. 如今学了javascript,我也能实现这个功能了,以下来显摆一下. 1.页面设计: ...
- cookie在vs又一次run的时候丢失
今天写个关于http cookie的demo,发现仅仅要vs又一次执行后cookie的值就会丢失,代码例如以下 protected void Page_Load(object sender, Even ...
- 左右db_block_size了解和实验
关于db_block_gets了解和实验 实验 一. 自己手动创建的小表 创建一个区大小为 40k SYS@ORCL>show parameter db_block_size NAME ...