Codeforces Round #237 (Div. 2)-D. Minesweeper 1D

原题链接

D. Minesweeper 1D
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares.

For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs.

Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.

Input

The first line contains sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square.

Output

Print a single integer — the number of ways Valera can fill the empty cells and get a correct field.

As the answer can be rather large, print it modulo 1000000007 (109 + 7).

Examples
input
?01???
output
4
input
?
output
2
input
**12
output
0
input
1
output
0
p[4] = {"0123*}, dp[k][i][j]开始表示第k个字符为p[i], 第k-1个字符为p[j]时有多少种情况

#include <bits/stdc++.h>
#define maxn 1000005
#define MOD 1000000007
using namespace std;
typedef long long ll;

char str[maxn];
ll dp[maxn][4][4];
void solve1(int i){
	(dp[i][0][0] += dp[i-1][0][1]) %= MOD;
	(dp[i][0][0] += dp[i-1][0][0]) %= MOD;
	(dp[i][0][1] += dp[i-1][1][3]) %= MOD;
} 
void solve2(int i){
	(dp[i][1][0] += dp[i-1][0][1]) %= MOD;
	(dp[i][1][0] += dp[i-1][0][0]) %= MOD;
	(dp[i][1][1] += dp[i-1][1][3]) %= MOD;
	(dp[i][1][3] += dp[i-1][3][1]) %= MOD;
	(dp[i][1][3] += dp[i-1][3][2]) %= MOD;	
	(dp[i][1][3] += dp[i-1][3][3]) %= MOD;
}
void solve3(int i){
    (dp[i][2][3] += dp[i-1][3][1]) %= MOD;
	(dp[i][2][3] += dp[i-1][3][2]) %= MOD;	
	(dp[i][2][3] += dp[i-1][3][3]) %= MOD;
}
void solve4(int i){
	(dp[i][3][1] += dp[i-1][1][1]) %= MOD;
	(dp[i][3][1] += dp[i-1][1][0]) %= MOD;
    (dp[i][3][2] += dp[i-1][2][3]) %= MOD;
    (dp[i][3][3] += dp[i-1][3][3]) %= MOD;
    (dp[i][3][3] += dp[i-1][3][1]) %= MOD;
    (dp[i][3][3] += dp[i-1][3][2]) %= MOD;
}
int main(){
	
//	freopen("in.txt", "r", stdin);
	scanf("%s", str+1);
	int len = strlen(str+1);
    if(str[1] == '2' || str[len] == '2'){
    	puts("0");
    	return 0;
    }
    if(len == 1){
    	if(str[1] == '?'){
    		puts("2");
    		return 0;
    	}
    	if(str[1] == '*' || str[1] == '0')
    	 puts("1");
    	else
    	 puts("0");
    	return 0;
    }
    if(str[1] == '0')
     dp[1][0][0] = 1;
    if(str[1] == '1')
     dp[1][1][0] = 1;
    if(str[1] == '*')
     dp[1][3][1] = 1;
    if(str[1] == '?'){
    	dp[1][0][0] = 1;
    	dp[1][1][0] = 1;
    	dp[1][3][1] = 1;
    }
	for(int i = 2; i <= len; i++){
		if(str[i] == '0'){
		    solve1(i);
		}
		if(str[i] == '1'){
			solve2(i);
		}
		if(str[i] == '2'){
			solve3(i);
		}
		if(str[i] == '*'){
			solve4(i);
		}
		if(str[i] == '?'){
			solve1(i);
			solve2(i);
			if(i != len) 
			solve3(i);
			solve4(i);
		}
	}
	ll ans = 0;
	for(int i = 0; i < 4; i++)
	 for(int j = 0; j < 4; j++)
	  (ans += dp[len][i][j]) %= MOD;
	if(str[len] == '?' || str[len] == '1'){
	    (ans = ans - dp[len-1][0][1] + MOD) %= MOD;
	    (ans = ans - dp[len-1][0][0] + MOD) %= MOD;
	    (ans = ans - dp[len-1][1][3] + MOD) %= MOD;
	}
	printf("%I64d\n", ans);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值