码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4758---Walk Through Squares(AC自动机+dp)

时间:2015-04-16 17:47:17      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:ac自动机   dp   

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

状态很好想
但我一开始想的状态是dp[i][j][k][sta] 长度为i,在节点j,含有k个R,状态为sta的方案数
这样数组略大,然后我想着用滚动数组,于是愉快的wa了
然后换了一个状态,dp[i][j][k][sta]含有i个D,j个R,在节点k,状态为sta的方案数,其实2种状态没什么区别的,因为D和R的数目是固定的,知道长度和R的个数,D个数就知道了,但是第二个状态比较省内存,不需要滚动数组,然后就建立自动机,转移即可

/*************************************************************************
    > File Name: hdu4758.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年04月02日 星期四 21时42分11秒
 ************************************************************************/

#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 double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int MAX_NODE = 210;
const int CHILD_NUM = 2;
const int mod = 1000000007;

int dp[110][110][210][4];
char buf[110];

struct AC_Automation
{
    int next[MAX_NODE][CHILD_NUM];
    int fail[MAX_NODE];
    int end[MAX_NODE];
    int root, L;

    int newnode()
    {
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            next[L][i] = -1;
        }
        end[L++] = 0;
        return L - 1;
    }

    int ID(char c)
    {
        return (c != ‘R‘);
    }

    void init()
    {
        L = 0;
        root = newnode();
    }

    void Build_Trie(char buf[], int id)
    {
        int now = root;
        int len = strlen(buf);
        for (int i = 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);
    }

    void Build_AC()
    {
        queue <int> qu;
        fail[root] = root;
        for (int i = 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())
        {
            int now = qu.front();
            qu.pop();
            end[now] |= end[fail[now]];
            for (int i = 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]);
                }
            }
        }
    }

    void solve(int n, int m)
    {
        memset(dp, 0, sizeof(dp));
        dp[0][0][0][0] = 1;
        for (int i = 0; i <= n; ++i)
        {
            for (int j = 0; j <= m; ++j)
            {
                for (int k = 0; k < L; ++k)
                {
                    for (int sta = 0; sta < 4; ++ sta)
                    {
                        if (dp[i][j][k][sta])
                        {
                            int node1 = next[k][0];
                            int node2 = 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;
                        }
                    }
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < L; ++i)
        {
            ans += dp[n][m][i][3];
            ans %= mod;
        }
        printf("%d\n", ans);
    }
}AC;

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int m, 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自动机+dp)

标签:ac自动机   dp   

原文地址:http://blog.csdn.net/guard_mine/article/details/45076639

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!