您的位置:首页 > 其它

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

2017-08-11 14:49 423 查看

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


[align=left]Problem Description[/align]



  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 ?
 

[align=left]Input[/align]
  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.

 

[align=left]Output[/align]
  For each test cases,print the answer MOD 1000000007 in one line.

 

[align=left]Sample Input[/align]

2
3 2
RRD
DDR
3 2
R
D

 

[align=left]Sample Output[/align]

1
10

 

[align=left]Source[/align]
[align=left]
[/align]
[align=left]题意:给你一个n*m的方阵,让你从左上角走到右下角,使得路线包含所给所有的模式串,问你有多少种方案。[/align]
[align=left]题解:dp[i][j][k][h]:表示走到(i,j)时匹配到自动机的k结点且包含模式串的状态为h的方案数。(注意题目给的是先列后行23333)。[/align]
[align=left]
[/align]
#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]
[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: