HDU 4758 Walk Through Squares (AC自动机 + 状压dp)

101 篇文章 1 订阅
15 篇文章 0 订阅

题意:

给你一个矩阵, 给你两个串, 问你从左上角走到右下角的路径中,包含这两个串的路径的方案数。

思路:

就是路径必须要匹配两种串。

可以考虑自动机上 状压。

很容易想到的是

dp[i][j][k][l][m]表示走第i 步, 在自动机j 结点, 目前有k 个D, l 个R, 包含两个串的状态为m的方案数。

但这样无论是 空间上 还是时间上 都无法承受。

那就只能把第一维删掉了

dp[i][j][k][l] 表示目前在自动机的i 节点, 有j 个D, k 个R, 状态为l 的方案数。

那么就很好转移了。


注意:

1. 因为删掉了一维,我们要注意dp 枚举的顺序, 我们应该先枚举 状态,   之后在是哪个节点。

2. 注意dp 枚举的上限, D的话 ,最多n-1 个  R的话 最多m-1个。  虽然我觉的不会有问题, 但没考虑上限确实wa掉了。 之后抱着试一试的心态改了过来, 就过了, 所以说dp 枚举上限要注意好。

总之dp 细节真的多。。。。。


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 200 + 10;

const int mod = 1000000007;

int get(char ch){
    if (ch == 'D') return 0;
    return 1;
}

struct Trie{
    int L, root;
    int next[maxn][2];
    int fail[maxn];
    int flag[maxn];
    int sum[maxn];
    int dp[maxn][101][101][4];


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

    int newnode(){
        for (int i = 0; i < 2; ++i){
            next[L][i] = -1;

        }
        flag[L] = 0;
        sum[L] = 0;
        return L++;
    }
    void insert(char* s,int id){
        int len = strlen(s);
        int nod = root;
        for (int i = 0; i < len; ++i){
            int id = get(s[i]);
            if (next[nod][id] == -1){
                next[nod][id] = newnode();
            }
            nod = next[nod][id];
        }
        flag[nod] = 1 << id;
//        printf("%d %d\n", flag[L], L);
        sum[nod] = 0;

    }
    void bfs(){
        queue<int>q;
        fail[root] = root;
        for (int i = 0; i < 2; ++i){

            if (next[root][i] == -1){
                next[root][i] = root;
            }
            else {
                fail[next[root][i] ] = root;
                q.push(next[root][i]);
            }
        }


        while(!q.empty()){
            int u = q.front();
            q.pop();
            sum[u] |= (flag[u] | sum[fail[u] ]);
            for (int i = 0; i < 2; ++i){
                if (next[u][i] == -1){
                    next[u][i] = next[fail[u] ][i];
                }
                else {
                    fail[next[u][i] ] = next[fail[u] ][i];
                    q.push(next[u][i]);
                }
            }
        }
    }


    void solve(int n,int m){

        memset(dp,0,sizeof dp);

        dp[0][0][0][0] = 1;
        int sp = m - 1 + n - 1;
        for (int i = 0; i < 4; ++i){
            for (int j = 0; j <= n-1; ++j){
                for (int k = 0; k <= m-1; ++k){
                    for (int l = 0; l < L; ++l){
                        if (!dp[l][j][k][i]) continue;
                        for (int o = 0; o < 2; ++o){
                            int nx = next[l][o];
                            int jj = j;
                            int kk = k;

                            if (o == 0)jj++;
                            else kk++;

                            if (jj  > n-1 || kk > m-1) continue;

                            dp[nx][jj][kk][i | sum[nx] ] = int((dp[nx][jj][kk][i | sum[nx] ] + (long long)dp[l][j][k][i]) % mod);
                        }
                    }
                }
            }
        }


        long long ans = 0;
        for (int i = 0; i < L; ++i){
            for (int j = 0; j <= n-1; ++j){
                int r = sp - j;
                if (r > m - 1) continue;
                ans = (ans + dp[i][j][r][3]) % mod;
            }
        }
        printf("%I64d\n", ans);
    }



};
Trie ac;


char s[maxn];


int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        int n, m;
        scanf("%d %d",&m, &n);
        ++n, ++m;
        ac.init();
        for (int i = 0; i < 2; ++i){
            scanf("%s", s);
            ac.insert(s, i);
        }
        ac.bfs();
        ac.solve(n, m);
    }

    return 0;
}



Walk Through Squares

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


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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6181  6180  6179  6178  6177 
 

Statistic |  Submit |  Discuss |  Note

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值