hdu4758(ac自动机,状态压缩dp)
2016-03-17 20:11
218 查看
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
![](http://acm.hdu.edu.cn/data/images/C479-1011-1.jpg)
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真是绝配。。。。
不过这类题目做多了越来越好理解了,详细讲解还是见这个类似的题:/article/8905780.html
这一题的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 [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; }
相关文章推荐
- [精]自己编写的一个40万本书籍查找下载系统
- Linux系统VNC配置实践总结
- 生成prefix.pch文件
- 权重随机算法
- css 选择器优先级的计算过程
- Kafka设计解析(三)- Kafka High Availability (下)
- 201603172004
- Codeforces 28B pSort(dfs,判断连通)
- android面试精华转载
- 从零开始_学_数据结构(零)——数据结构总述
- JAR包的JDK版本查看与设置
- centos下添加的端口不能访问(防火墙关闭)
- <context:component-scan>使用说明
- 双击导航栏自动滑动ListView到顶部
- 集诸多黑科技于一身,三星Notebook 9系列如何带来另类体验?
- Quartz 2D简单介绍
- Codeforces Round #343 (Div. 2)
- 亲测三种办法解决:List用Remove方法报错
- 使用sprintf函数连续保存图片问题。
- 机器学习算法之随机森林(Random Forest)