Hdu4758 walk through squares (AC automation + dp)

Source: Internet
Author: User
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
On the beaming day of 60th Anniversary of njust, as a military college which was Second Artillery Emy 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 conducting 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?
Inputthe 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.

Outputfor each test cases, print the answer MOD 1000000007 in one line.

Sample Input
23 2RRDDDR3 2RD

Sample output
110

Source2013 ACM/ICPC Asia Regional Nanjing online
Recommendliuyiding | we have carefully selected several similar problems for you: 5017 5016 5015 5014
Top-layer model: the AC automatic mechanism is used to process state transfer, similar to KMP, but KMP is used to process a mode string, while the AC automatic mechanism is used to process a bunch of mode strings, for each State, the next transition state is also shown in the figure indicated by the automatic machine.
Solution: DP [I] [J] [k] [p] indicates that the status of the row J in the I is K, and the total number of solutions for getting two strings and not getting them is not. One thing to note is that a location may be represented by multiple ending States. Therefore, you must add all values that serve as the public prefix of the ending state. The rest is easy.
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define mod 1000000007using 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 99DRDDRDDDRD*/




Hdu4758 walk through squares (AC automation + dp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.