HDU4758AC自动机DP

来源:互联网 发布:u盘恢复数据要多少钱 编辑:程序博客网 时间:2024/06/05 07:57

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

en或一下是关键(广搜保证正确性),AC自动机套DP的基本方法:如果要出现模板串就状态压缩。如果要不出现,DP时就不要走到那个点。然后fail直接接儿子下面是DP的关键。
要输出方案的话,就DFS一遍。

using namespace std;#include<cstdio>#include<iostream>#include<queue>#include<cstring>#define r(x) scanf("%d",&x)#define F(i,a,b) for(int i=a;i<=b;i++)#define N 204#define ll long longint dp[102][102][242][4];char s1[N*2],s2[N*2];int r1[N*2],r2[N*2],r3[N*2];int fail[N*2];int en[N*2];ll ans,mod=1e9+7;int n,m,l1,l2;bool flag;int son[N*2][2];int sz,root;bool chg(char x){    if(x=='R') return 1;else return 0;}void swp(){    for(int i=1;i<=l1;i++) r3[i]=r1[i];    for(int i=1;i<=l2;i++) r1[i]=r2[i];    for(int i=1;i<=l1;i++) r2[i]=r3[i];    swap(l1,l2); }bool vis[N*2];void init(){    memset(son,0,sizeof(son));    memset(dp,0,sizeof(dp));    memset(fail,0,sizeof(fail));    sz=1;root=1;    memset(vis,0,sizeof(vis));    l1=0;l2=0;    flag=0;n=m=0;ans=0;    memset(en,0,sizeof(en));}void build(void){    queue<int>q;    while(!q.empty())q.pop();    q.push(1);    fail[1]=0;    while(!q.empty())    {        int ind=q.front();        q.pop();        for(int i=0;i<=1;i++)        if(son[ind][i])        {            int it=fail[ind];            while(!son[it][i]&&it) it=fail[it];            if(!it)it=1;else it=son[it][i];            fail[son[ind][i]]=it;            en[son[ind][i]]|=en[it];//之前的特判,特盘错了,那是不对的。             if(!vis[son[ind][i]])q.push(son[ind][i]);        }    }    for(int i=1;i<=sz;i++)    {        for(int j=0;j<=1;j++)        {            if(!son[i][j])            {                int it=fail[i];                while(it&&!son[it][j])it=fail[it];                if(!it)it++;else it=son[it][j];                son[i][j]=it;            }        }    }}void onedance(){    init();    r(n);r(m);scanf("%s",s1);scanf("%s",s2);    l1=strlen(s1);l2=strlen(s2);    F(i,0,l1-1){r1[i+1]=chg(s1[i]);}    F(j,0,l2-1){r2[j+1]=chg(s2[j]);}    {        int p=1;        for(int i=1;i<=l1;i++)        {            if(!son[p][r1[i]])son[p][r1[i]]=++sz;            p=son[p][r1[i]];        }        en[p]=1;    }    int p=1;    for(int i=1;i<=l2;i++)    {        if(!son[p][r2[i]])son[p][r2[i]]=++sz;        p=son[p][r2[i]];    }    en[p]=2;    build();    dp[0][0][1][0]=1;    for(int len=0;len<n+m;len++)    {        for(int i=max(len-n,0);i<=min(len,m);i++)//DP范围         {            for(int k=0;k<=3;k++)            {                for(int j=1;j<=sz;j++)                {                    for(int sy=0;sy<=1;sy++)                    {                        int p=son[j][sy];                        dp[len-i+sy][i+1-sy][p][k|en[p]]=(dp[len-i+sy][i+1-sy][p][k|en[p]]+dp[len-i][i][j][k])%mod;                    }                }            }        }    }    ans=0;    for(int i=1;i<=sz;i++)    {        ans=(ans+dp[n][m][i][3])%mod;    }    cout<<ans<<endl;}int main(){    int Despacito;r(Despacito);while(Despacito--)onedance();}
原创粉丝点击