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

Walk Through Squares

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


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

题意:给你一个n*m的方阵,让你从左上角走到右下角,使得路线包含所给所有的模式串,问你有多少种方案。
题解:dp[i][j][k][h]:表示走到(i,j)时匹配到自动机的k结点且包含模式串的状态为h的方案数。(注意题目给的是先列后行23333)。

#include<map>        
#include<stack>        
#include<queue>        
#include<vector>        
#include<math.h>        
#include<stdio.h>        
#include<iostream>        
#include<string.h>        
#include<stdlib.h>        
#include<algorithm>        
using namespace std;        
typedef long long  ll;        
#define inf 1000000000       
#define mod 1000000007         
#define  maxn  550      
#define  lowbit(x) (x&-x)        
#define  eps 1e-10    
int pre[maxn],a[maxn][2],flag[maxn],size,n,m,dp[105][105][305][4];
char s[125];  
queue<int>q;  
struct node  
{  
    ll mat[105][105];  
}b,d;  
int c(char x)  
{  
    if(x=='R')return 0;  
    if(x=='D')return 1;   
}  
void insert(int num)  
{  
    int i,len=strlen(s),now=0,cnt=0;  
    for(i=0;i<len;i++)  
    {  
        int v=c(s[i]);  
        if(!a[now][v])  
        {  
            flag[size]=0;  
            memset(a[size],0,sizeof(a[size]));  
            a[now][v]=size++;  
        }  
        now=a[now][v];  
    }  
	flag[now]=(1<<num);  
}  
void build_fail()  
{  
    int now,i;  
    for(i=0;i<2;i++)  
    {  
        int tmp=a[0][i];  
        if(tmp)  
            pre[tmp]=0,q.push(tmp);  
    }  
    while(q.empty()==0)  
    {  
        now=q.front();  
        q.pop();  
        if(flag[pre[now]])  
            flag[now]|=flag[pre[now]];  
        for(i=0;i<2;i++)  
        {  
            if(a[now][i]==0)  
            {  
                a[now][i]=a[pre[now]][i];  
                continue;  
            }  
            pre[a[now][i]]=a[pre[now]][i];  
            q.push(a[now][i]);  
        }     
    }  
} 
void work()
{
	int i,j,k,h,ans,tmp;
	memset(dp,0,sizeof(dp));
	dp[0][0][0][0]=1;
	for(i=0;i<=m;i++)
		for(j=0;j<=n;j++)
			for(k=0;k<size;k++)
				for(h=0;h<4;h++)
				{
					if(dp[i][j][k][h]==0)
						continue;
					dp[i+1][j][a[k][1]][h|flag[a[k][1]]]=(dp[i+1][j][a[k][1]][h|flag[a[k][1]]]+dp[i][j][k][h])%mod;
					dp[i][j+1][a[k][0]][h|flag[a[k][0]]]=(dp[i][j+1][a[k][0]][h|flag[a[k][0]]]+dp[i][j][k][h])%mod;
				}
	 ans=0;
	 for(i=0;i<size;i++)
		 ans=(ans+dp[m][n][i][3])%mod;
	 printf("%d\n",ans);
}
int  main(void)  
{  
    int i,T;  
	scanf("%d",&T);
    while(T--)  
    {  
        size=1;pre[0]=0;flag[0]=0;  
        memset(a[0],0,sizeof(a[0]));  
        memset(pre,0,sizeof(pre));  
		scanf("%d%d",&m,&n);
		swap(m,n);
        for(i=0;i<2;i++)  
        {          
            scanf("%s",s);  
            insert(i);  
        }  
        build_fail();  
		work();
    }  
    return 0;  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值