hdu4758(ac自动机,状态压缩dp)

Walk Through Squares

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


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


ac自动机+状态压缩dp真是绝配。。。。

不过这类题目做多了越来越好理解了,详细讲解还是见这个类似的题:http://blog.csdn.net/martinue/article/details/50895963

这一题的dp[i][j][k][h]表示走到了i行j列在ac自动机里面的第k个节点用到题目所给的字符串已经匹配的状态为h的所有情况数。


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const int NODE=200,CH=2,MOD=1000000007;
int m,n;
int idx(char x)
{
    if(x=='R')
        return 0;
    return 1;
}
int ch[NODE][CH],f[NODE],val[NODE],sz,dp[105][105][205][4];
int node()
{
    memset(ch[sz],0,sizeof(ch[sz]));
    val[sz]=0;
    return sz++;
}
void init()///总是忘记这个。。。。。
{
    sz=0;
    node();
    memset(f,0,sizeof(f));
}
void ins(char *s,int i)
{
    int u=0;
    for(; *s; s++)
    {
        int c=idx(*s);
        if(!ch[u][c])
            ch[u][c]=node();
        u=ch[u][c];
    }
    val[u]|=1<<i;
}
int queu[110];
void getfail()
{
    int l=0,r=0;
    queu[r++]=0;
    while(l<r)
    {
        int p=queu[l++];
        for(int i=0; i<CH; i++)
        {
            if(!ch[p][i])
                ch[p][i]=ch[f[p]][i];
            else
            {
                int q=ch[p][i];
                if(p)
                    f[q]=ch[f[p]][i];
                val[q]|=val[f[q]];
                queu[r++]=q;
            }
        }
    }
}

void solve()
{
    memset(dp,0,sizeof(dp));
    dp[0][0][0][0]=1;
    for(int i=0; i<=n; i++)///i行
        for(int j=0; j<=m; j++)///j列
            for(int k=0; k<sz; k++)
                for(int h=0; h<4; h++)
                    if(dp[i][j][k][h])
                    {
                        dp[i+1][j][ch[k][1]][h|val[ch[k][1]]]+=dp[i][j][k][h];///行数加一,下一个节点必然是'D'
                        dp[i+1][j][ch[k][1]][h|val[ch[k][1]]]%=MOD;
                        dp[i][j+1][ch[k][0]][h|val[ch[k][0]]]+=dp[i][j][k][h];///列数加一,下一个节点必然是'R'
                        dp[i][j+1][ch[k][0]][h|val[ch[k][0]]]%=MOD;
                    }
    int ans=0;
    for(int i=0; i<sz; i++)
    {
        ans+=dp[n][m][i][3];
        ans%=MOD;
    }
    printf("%d\n",ans);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&m,&n);///注意这题表示的意思是m列n行!!!
        for(int i=0; i<2; i++)
        {
            char a[110];
            scanf("%s",a);
            ins(a,i);
        }
        getfail();
        solve();
    }
    return 0;
}


转载于:https://www.cnblogs.com/martinue/p/5490446.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值