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 ≤ 10^6), 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 (10^9 + 7).
Example
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**1, 001***, 001*2*, 001*10.
大致题意:有若干个连续的格子,每个格子里有这么几种符号”*”, “0”, “1” ,”2”,”?”
“*”表示当前格子里埋着地雷,“0”表示左右两个相连的格子里都没有地雷,“1”表示左右两个相连的格子里有一个有地雷,“2”表示左右两个相连的格子里有两个地雷,“?”表示你可以选择前面的任意一个。
问你有多少种合法的方式。
思路:很巧妙的dp,dp[i][j]表示位置i上的状态为j时有多少种合法方案,具体看下面代码。
代码如下
#include
#include
#include
using namespace std;
const int maxn=1e6+5;
const int mod=1e9+7;
long long int dp[maxn][5];//5种状态
/*
0 str[i]为“0”
1 str[i]为“1” 前一个是地雷,后一个不是
2 str[i]为“1” 前一个不是地雷,后一个是
3 str[i]为“*” 地雷
4 两个地雷,即str[i]为“2”
*/
char str[maxn];
int main()
{
memset(dp,0,sizeof(dp));
scanf("%s",&str);
int len=strlen(str);
if(str[0]=='0') dp[0][0]=1;
else if(str[0]=='1') dp[0][2]=1;
else if(str[0]=='*') dp[0][3]=1;
else if(str[0]=='?')
{
dp[0][0]=1;
dp[0][2]=1;
dp[0][3]=1;
}
for(int i=1;iif(str[i]=='0')
dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
else if(str[i]=='1')
{
dp[i][1]=dp[i-1][3];
dp[i][2]=(dp[i-1][0]+dp[i-1][1])%mod;
}
else if(str[i]=='2')
dp[i][4]=dp[i-1][3];
else if(str[i]=='*')
dp[i][3]=(dp[i-1][2]+dp[i-1][4]+dp[i-1][3])%mod;
else if(str[i]=='?')
{
dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
dp[i][1]=dp[i-1][3];
dp[i][2]=(dp[i-1][0]+dp[i-1][1])%mod;
dp[i][4]=dp[i-1][3];
dp[i][3]=(dp[i-1][2]+dp[i-1][4]+dp[i-1][3])%mod;
}
}
len--;
printf("%lld\n",(dp[len][0]+dp[len][1]+dp[len][3])%mod);
}