Walk Through Squares

Time limit:2000ms Memory limit:65535kB

链接

http://acm.hdu.edu.cn/showproblem.php?pid=4758

题意

从左上角到右下角为M*N的图,每次只能向下向右行动一步,给出两种必须要走的步法,问一共有多少种走法满足给定的要求的走法?

思路

题解

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

typedef long long ll;

const int maxn = 205;
const int kind = 2;
const int mod = 1e9+7;

int root, cnt;
int nxt[maxn][kind];
int fail[maxn];
int mrk[maxn];

int dic[128];

int newnode ()
{
    ++cnt;
    for (int i = 0; i < kind; ++i) {
        nxt[cnt][i] = -1;
    }
    mrk[cnt] = 0;
    return cnt;
}

void ac_init ()
{
    cnt = -1;
    root = newnode();
}

void wd_insert (char str[], int p)
{
    int now = root;
    int len = strlen(str);
    for (int i = 0; i < len; ++i) {
        if (nxt[now][dic[str[i]]] == -1) {
            nxt[now][dic[str[i]]] = newnode();
        }
        now = nxt[now][dic[str[i]]];
    }
    mrk[now] = p;
}

void build ()
{
    queue<int> q;
    fail[root] = root;
    for (int i = 0; i < kind; ++i) {
        if (nxt[root][i] == -1) {
            nxt[root][i] = root;
        }
        else {
            fail[nxt[root][i]] = root;
            q.push(nxt[root][i]);
        }
    }
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        mrk[now] |= mrk[fail[now]];
        for (int i = 0; i < kind; ++i) {
            if (nxt[now][i] == -1) {
                nxt[now][i] = nxt[fail[now]][i];
            }
            else {
                fail[nxt[now][i]] = nxt[fail[now]][i];
                q.push(nxt[now][i]);
            }
        }
    }
}

void init ()
{
    dic['D'] = 0;
    dic['R'] = 1;
}

int n, m;

char str[105];

int dp[105][105][205][5];

int main ()
{
    int t;
    init();
    scanf("%d", &t);
    while (t--) {
        ac_init();
        scanf("%d %d", &m, &n);
        scanf("%s", str);
        wd_insert(str, 1);
        scanf("%s", str);
        wd_insert(str, 2);
        build();
        memset(dp, 0, sizeof(dp));
        dp[0][0][0][0] = 1;
        for (int i = 0; i <= m; ++i) {
            for (int j = 0; j <= n; ++j) {
                for (int u = 0; u <= cnt; ++u) {
                    for (int k = 0; k < 4; ++k) {
                        if (dp[i][j][u][k] == 0) {
                            continue;
                        }
                        if (i != m) {
                            dp[i+1][j][nxt[u][1]][k|mrk[nxt[u][1]]] += dp[i][j][u][k];
                            dp[i+1][j][nxt[u][1]][k|mrk[nxt[u][1]]] %= mod;
                        }
                        if (j != n) {
                            dp[i][j+1][nxt[u][0]][k|mrk[nxt[u][0]]] += dp[i][j][u][k];
                            dp[i][j+1][nxt[u][0]][k|mrk[nxt[u][0]]] %= mod;
                        }
                    }
                }
            }
        }
        ll res = 0;
        for (int i = 0; i <= cnt; ++i) {
            res += dp[m][n][i][3];
            res %= mod;
        }
        cout << res << '\n';
    }
    return 0;
}

发表评论