HDU4758-Walk Through Squares

Walk Through Squares

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


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
 

Recommend
liuyiding
 

题意:有个n×m的格子,给你两种走法,每种走法都是一个包含D或R的序列,D表示向下走R表示向右走。问从左上角走到右下角的走法有多少种走法包含那两种走法

解题思路:用两种走法的序列构造AC自动机,dp[i][j][S][k]表示D用了i个R用了j个,且当前走到自动机的S结点,已经包含的走法的状态集合是k的方案数



#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

char ch[205];
int dp[110][110][220][4], n, m;

struct Trie
{
	int next[500][2], fail[500], flag[500];
	int root, tot;
	int newnode()
	{
		for (int i = 0; i < 2; i++) next[tot][i] = -1;
		flag[tot++] = 0;
		return tot - 1;
	}
	void init()
	{
		tot = 0;
		root = newnode();
	}
	int get(char ch)
	{
		if (ch == 'R') return 0;
		else return 1;
	}
	void insert(char ch[], int id)
	{
		int k = root;
		for (int i = 0; ch[i]; i++)
		{
			if (next[k][get(ch[i])] == -1) next[k][get(ch[i])] = newnode();
			k = next[k][get(ch[i])];
		}
		flag[k] = (1 << id);
	}
	void build()
	{
		queue<int>q;
		fail[root] = root;
		for (int i = 0; i < 2; i++)
		{
			if (next[root][i] == -1) next[root][i] = root;
			else
			{
				fail[next[root][i]] = root;
				q.push(next[root][i]);
			}
		}
		while (!q.empty())
		{
			int pre = q.front();
			q.pop();
			flag[pre] |= flag[fail[pre]];
			for (int i = 0; i < 2; i++)
			{
				if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i];
				else
				{
					fail[next[pre][i]] = next[fail[pre]][i];
					q.push(next[pre][i]);
				}
			}
		}
	}
	void solve()
	{
		for (int i = 0; i <= n; i++)
			for (int j = 0; j <= m; j++)
				for (int k = 0; k < tot; k++)
					for (int p = 0; p < 4; p++)
						dp[i][j][k][p] = 0;
		dp[0][0][0][0] = 1;
		for (int i = 0; i <= n; i++)
			for (int j = 0; j <= m; j++)
				for (int k = 0; k < tot; k++)
					for (int p = 0; p < 4; p++)
						if (dp[i][j][k][p])
						{
							int nt1 = next[k][0], nt2 = next[k][1];
							if (i < n) (dp[i + 1][j][nt1][p | flag[nt1]] += dp[i][j][k][p]) %= mod;
							if (j < m) (dp[i][j + 1][nt2][p | flag[nt2]] += dp[i][j][k][p]) %= mod;
						}
		int ans = 0;
		for (int i = 0; i < tot; i++) (ans += dp[n][m][i][3]) %= mod;
		printf("%d\n", ans);
	}
	void debug()
	{
		for (int i = 0; i < tot; i++)
		{
			printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], flag[i]);
			for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);
			printf("]\n");
		}
	}
}ac;

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &m);
		ac.init();
		for (int i = 0; i < 2; i++)
		{
			scanf("%s", &ch);
			ac.insert(ch, i);
		}
		ac.build();
		ac.solve();
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值