hdu4758 Walk Through squares (ac own active machine +DP)

Source: Internet
Author: User

Walk Through SquaresTime 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 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?
Inputthe First line contains a number T, (t was about including, small test cases, large ones) denoting the NUM ber of 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.

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

Sample Input
2RD 2RRDDDR3

Sample Output
110

Source

Field=problem&key=2013+acm%2ficpc+asia+regional+nanjing+online&source=1&searchmode=source ">2013 ACM/ICPC Asia Regional Nanjing Online
recommendliuyiding | We have carefully selected several similar problems for you:5017 5016 5015

pid=5014 "target=" _blank ">5014

pid=5013 "target=" _blank ">5013
Top-level model: AC own active machine, is used to deal with the state transfer, and KMP similar. Just KMP is dealing with a pattern string, and AC's own active machine is used to process a bunch of pattern strings, and for each state the state of the next transition is also on the diagram represented by its own active machine.
problem-solving ideas: Dp[i][j][k][p] said to the first row of the J column of their own active machine status K, two strings and not take the total number of programs. One thing to note is that a location may be represented by multiple finalization states, so add the values of all public prefixes that are the end state. The rest is very 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++) {                    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 own active machine +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.