HDU 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)

Walk Through Squares

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 200    Accepted Submission(s): 57

Problem Description

  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.  

 

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
2013 ACM/ICPC Asia Regional Nanjing Online  

 

Recommend liuyiding  

 

 

 

 

太伤了。

网络赛的时候没有过这题,都写对了,就是一直WA

 

比赛结束后问了下世界冠军cxlove,看了一眼发现是建立AC自动机的时候,fail的end要传递下,加一句话就过了,TAT

 

使用AC自动机+DP。

dp[x][y][i][k] 四维DP,表示R的个数是x,D的个数是y, i是在AC自动机上的节点编号。k 是状态压缩。

  1 /* ***********************************************
  2 Author        :kuangbin
  3 Created Time  :2013/9/21 星期六 15:57:51
  4 File Name     :2013南京网络赛\1011.cpp
  5 ************************************************ */
  6 
  7 #pragma comment(linker, "/STACK:1024000000,1024000000")
  8 #include <stdio.h>
  9 #include <string.h>
 10 #include <iostream>
 11 #include <algorithm>
 12 #include <vector>
 13 #include <queue>
 14 #include <set>
 15 #include <map>
 16 #include <string>
 17 #include <math.h>
 18 #include <stdlib.h>
 19 #include <time.h>
 20 using namespace std;
 21 const int MOD = 1e9+7;
 22 
 23 int dp[110][110][220][4];
 24 int n,m;
 25 //struct Trie
 26 //{
 27     int next[420][2],fail[420],end[420];
 28     int root,L;
 29     inline int change(char ch)
 30     {
 31         if(ch == 'R')return 0;
 32         else return 1;
 33     }
 34     inline int newnode()
 35     {
 36         for(int i = 0;i < 2;i++)
 37             next[L][i] = -1;
 38         end[L++] = 0;
 39         return L-1;
 40     }
 41     inline void init()
 42     {
 43         L = 0;
 44         root = newnode();
 45     }
 46     inline void insert(char buf[],int id)
 47     {
 48         int len = strlen(buf);
 49         int now = root;
 50         for(int i = 0;i < len;i++)
 51         {
 52             if(next[now][change(buf[i])] == -1)
 53                 next[now][change(buf[i])] = newnode();
 54             now = next[now][change(buf[i])];
 55         }
 56         end[now] |= (1<<id);
 57     }
 58     inline void build()
 59     {
 60         queue<int>Q;
 61         fail[root] = root;
 62         for(int i = 0;i < 2;i++)
 63             if(next[root][i] == -1)
 64                 next[root][i] = root;
 65             else
 66             {
 67                 fail[next[root][i]] = root;
 68                 Q.push(next[root][i]);
 69             }
 70         while( !Q.empty() )
 71         {
 72             int now = Q.front();
 73             Q.pop();
 74             end[now] |= end[fail[now]];
 75             for(int i = 0;i < 2;i++)
 76                 if(next[now][i] == -1)
 77                     next[now][i] = next[fail[now]][i];
 78                 else
 79                 {
 80                     fail[next[now][i]]=next[fail[now]][i];
 81                     Q.push(next[now][i]);
 82                 }
 83         }
 84     }
 85     inline int solve()
 86     {
 87         dp[0][0][0][0] = 1;
 88         int ret = 0;
 89         
 90             for(int x = 0;x <= n;x++)
 91                 for(int y = 0;y <= m;y++)
 92                     for(int i = 0;i < L;i++)
 93 
 94                     for(int k = 0;k < 4;k++)
 95 
 96                     {
 97                         if(dp[x][y][i][k] == 0)continue;
 98                         if(x < n)
 99                         {
100                             int nxt = next[i][0];
101                             dp[x+1][y][nxt][k|end[nxt]] += dp[x][y][i][k];
102                             if(dp[x+1][y][nxt][k|end[nxt]] >= MOD)
103                                 dp[x+1][y][nxt][k|end[nxt]] -= MOD;
104                         }
105                         if(y < m)
106                         {
107                             int nxt = next[i][1];
108                             dp[x][y+1][nxt][k|end[nxt]] += dp[x][y][i][k];
109                             if(dp[x][y+1][nxt][k|end[nxt]] >= MOD)
110                                 dp[x][y+1][nxt][k|end[nxt]] -= MOD;
111                         }
112                     }
113         for(int i = 0;i < L;i++)
114         {
115             ret += dp[n][m][i][3];
116             if(ret >= MOD)ret -= MOD;
117         }
118         
119 
120         return ret;
121 
122 
123     }
124 //};
125 //Trie ac;
126 char str[210];
127 
128 int main()
129 {
130     //freopen("in.txt","r",stdin);
131     //freopen("out.txt","w",stdout);
132     int T;
133     scanf("%d",&T);
134     while(T--)
135     {
136         scanf("%d%d",&n,&m);
137         init();
138         for(int i = 0;i < 2;i++)
139         {
140             scanf("%s",str);
141             insert(str,i);
142         }
143         build();
144         for(int i = 0;i <= n;i++)
145             for(int j = 0;j <= m;j++)
146                 for(int x = 0; x < L;x++)
147                     for(int y = 0;y < 4;y++)
148                         dp[i][j][x][y] = 0;
149         printf("%d\n",solve());
150     }
151     return 0;
152 }

 

 

 

 

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3332874.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞