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
Problem Description
![](http://acm.hdu.edu.cn/data/images/C479-1011-1.jpg)
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.
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
顶层模型:AC自己主动机,就是用来处理状态转移的,和kmp类似。仅仅只是kmp是处理一个模式串,而AC自己主动机用来处理一堆模式串,对于每一状态而言下一个转移的状态也在自己主动机所表示的图上。
解题思路:dp[i][j][k][p]表示到第i行第j列自己主动机状态为k,二个串取和没取总的方案数。
要注意一个事情,就是一个位置可能由多个终结状态表示,所以要加上全部作为终结状态的公共前缀的值。
剩下的就非常easy了。
#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 */