Problem Description
On the beaming day of 60th anniversary of Njust, as a military college which was Second artillery Academy of Harbin Milita Ry Engineering Institute before, queue Phalanx is a special landscape.
Here's a m*n rectangle, and this one can be divided to m*n squares which is 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 is all connected to their adjacent nodes. and actual queue Phalanx would go along the edges.
The ID of the first node,the one in Top-left corner,is 1. And the ID increases line from line first, and then by column in turn, as shown in the figure above.
For every node,there is and viable paths:
(1) Go downward, indicated by ' D ';
(2) Go right, indicated by ' R ';
The current mission are, each queue phalanx have to walk from the Left-top node the most to the Right-bottom node whose ID Is (m+1) * (n+1).
In order to make a more aesthetic marching, each queue phalanx have to conduct the necessary actions. Let ' s define the action:
An action was started from a node to go for a specified travel mode.
So, the other actions must show up in the the-the-1 to (m+1) * (n+1).
For example, as-a 3*2 rectangle, figure below:
01–02–03–04
|| || || ||
05–06–07–08
|| || || ||
09–10–11–12
Assume that the both actions are (1) RRD (2) DDR
As a result, there is only one way:rrddr. Briefly, you can not find another sequence containing these both strings at the same time.
If given the N, M and the actions, can calculate the total ways of walking from node to the Right-bottom node?
Input
The first line contains a number T, (t was about including small test cases and large ones) denoting the number O f the test cases.
For each test cases,the first line contains the 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 lines each contains a string which contains only ' R ' and ' D '. The length of string would not be exceed 100. We ensure there is no empty strings and the strings are different.
Output
For each test cases,print the answer MOD 1000000007 on one line.
Sample Input
2
3 2
Rrd
Ddr
3 2
R
D
Sample Output
1
10
Source
ACM/ICPC Asia Regional Nanjing Online
The state is very good to think
But the state I was thinking of was dp[i][j][k][sta] length I, in Node J, with K R, number of scenarios with STA status
So the array was slightly larger, and then I thought about using a scrolling array, so happy WA was
Then changed a state, Dp[i][j][k][sta] contains I d,j r, in node K, the status of the STA program number, in fact, 2 states no difference, because the number of D and R is fixed, know the length and number of R, D number will know, but the second state compared to save memory, Do not need to scroll the array, then set up the automata, transfer
/************************************************************************* > File Name:hdu4758.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 April 02 Thursday 21:42 11 seconds ******************************** ****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Const intMax_node = About;Const intChild_num =2;Const intMoD =1000000007;intdp[ the][ the][ About][4];Charbuf[ the];structac_automation{intNext[max_node][child_num];intFail[max_node];intEnd[max_node];intRoot, L;intNewNode () { for(inti =0; i < Child_num; ++i) {Next[l][i] =-1; } end[l++] =0;returnL1; }intID (Charc) {return(c! =' R '); }voidInit () {L =0; root = NewNode (); }voidBuild_trie (CharBuf[],intID) {intnow = root;intLen =strlen(BUF); for(inti =0; i < Len; ++i) {if(Next[now][id (buf[i]) = =-1) {Next[now][id (buf[i])] = NewNode (); } now = Next[now][id (Buf[i]); } End[now] |= (1<< ID); }voidBuild_ac () { Queue <int>Qu Fail[root] = root; for(inti =0; i < Child_num; ++i) {if(Next[root][i] = =-1) {Next[root][i] = root; }Else{Fail[next[root][i]] = root; Qu.push (Next[root][i]); } } while(!qu.empty ()) {intnow = Qu.front (); Qu.pop (); End[now] |= End[fail[now]; for(inti =0; i < Child_num; ++i) {if(Next[now][i] = =-1) {Next[now][i] = Next[fail[now]][i]; }Else{Fail[next[now][i]] = next[fail[now]][i]; Qu.push (Next[now][i]); } } } }voidSolveintNintm) {memset(DP,0,sizeof(DP)); dp[0][0][0][0] =1; for(inti =0; I <= N; ++i) { for(intj =0; J <= M; ++J) { for(intK =0; K < L; ++K) { for(intSTA =0; STA <4; + + STA) {if(Dp[i][j][k][sta]) {intNode1 = next[k][0];intNode2 = next[k][1]; Dp[i][j +1][node1][sta | end[node1]] + = Dp[i][j][k][sta]; Dp[i +1][j][node2][sta | end[node2]] + = Dp[i][j][k][sta]; Dp[i][j +1][node1][sta | end[node1]]%= mod; Dp[i +1][j][node2][sta | end[node2]]%= mod; } } } } }intAns =0; for(inti =0; i < L; ++i) {ans + = dp[n][m][i][3]; Ans%= MoD; }printf("%d\n", ans); }}ac;intMain () {intTscanf("%d", &t); while(t--) {intM, N;scanf("%d%d", &m, &n); Ac.init ();scanf('%s ', buf); AC. Build_trie (BUF,0);scanf('%s ', buf); AC. Build_trie (BUF,1); AC. Build_ac (); Ac.solve (n, m); }return 0;}
hdu4758---Walk Through squares (ac automaton +DP)