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

时间:2022-12-22 10:12:30
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
Note

In the first test sample you can get the following correct fields: 001**1001***001*2*001*10.



题目大意:给你一行字符串,"1"意味着左右两边有一边是“*”炸弹,“2”意味着左右两边都是“*”炸弹,“0”意味着左右不可能是炸弹,问你一共有多少种排列方式,结果可能特别大,所以要求1e9+7的模

解题思路:这是一道DP题,根据当前位置出现的字符串设定转移方程

dp[i][0]表示i位置为0的合法性
dp[i][1]表示i位置为1,i-1位置为*的合法性
dp[i][2]表示i位置为2合法性
dp[i][3]表示i位置为*的合法性
dp[i][4]表示i位置为1,i+1位置为*的合法性

#include<iostream>    
#include<cstdio>  
#include<stdio.h>  
#include<cstring>    
#include<cstdio>    
#include<climits>    
#include<cmath>   
#include<vector>  
#include <bitset>  
#include<algorithm>    
#include <queue>  
#include<map>  
#include<stack>


using namespace std;

long long int MOD = 1e9 + 7;
string str1;
char str[1000005];
long long int dp[1000005][10];
int n, i;
int main()
{
	cin >> str1;
	n = str1.length();
	for (i = 0; i < n; i++)
	{
		str[i + 1] = str1[i];
	}
	memset(dp, 0, sizeof(dp));
	if (str[1] == '?')
	{
		dp[1][0] = 1;
		dp[1][3] = 1;
		dp[1][4] = 1;
	}
	else if (str[1] == '1')
	{
		dp[1][4] = 1;
	}
	else if (str[1] == '*')
	{
		dp[1][3] = 1;
	}
	else if (str[1] == '0')
	{
		dp[1][0] = 1;
	}
	for (i = 2; i <= n; i++)
	{
		if (str[i] == '0')
		{
			dp[i][0] = (dp[i - 1][1] + dp[i - 1][0]) % MOD;
		}
		else if (str[i] == '?')
		{
			dp[i][0] = (dp[i - 1][1] + dp[i - 1][0]) % MOD;
			dp[i][1] = dp[i - 1][3] % MOD;
			dp[i][4] = (dp[i - 1][0] + dp[i - 1][1]) % MOD;
			dp[i][2] = (dp[i - 1][3]) % MOD;
			dp[i][3] = (dp[i - 1][4] + dp[i - 1][3] + dp[i - 1][2]) % MOD;
		}
		else if (str[i] == '1')
		{
			dp[i][1] = dp[i - 1][3] % MOD;
			dp[i][4] = (dp[i - 1][0] + dp[i - 1][1]) % MOD;
		}
		else if (str[i] == '2')
		{
			dp[i][2] = (dp[i - 1][3]) % MOD;
		}
		else if (str[i] == '*')
		{
			dp[i][3] = (dp[i - 1][4] + dp[i - 1][3] + dp[i - 1][2]) % MOD;
		}
	}
	if (str[n] == '0')
	{
		cout << dp[n][0] % MOD << endl;
	}
	else if (str[n] == '1')
	{
		cout << (dp[n][0] + dp[n][1] + dp[n][3]) % MOD << endl;
	}
	else if (str[n] == '2')
	{
		cout << 0 << endl;
	}
	else if (str[n] == '*')
	{
		cout << (dp[n][4] + dp[n][3]) % MOD << endl;
	}
	else
	{
		cout << (dp[n][0] + dp[n][1] + dp[n][3]) % MOD << endl;
	}
}