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.
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.
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).
?01???
4
?
2
**12
0
1
0
In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10.
题意:给一串字符,‘0’表示该点左右都没有地雷,‘1’表示左右两边有一个,‘2’表示左右两边都有,'*'表示该点本身就是雷,‘?’表示该点情况不确定,现在题目问满足输入的不同情况有多少种;
思路:我们把每个点和他前面的点联系起来,类似递推;
解释几个标记:
0:该点左右两边都没有雷;
1:该点左边有雷;(1和2相当于把输入的‘1’这种情况展开)
2:该点右边有雷;
3:该点左右两边都有雷;
4:该点自身为雷;
那么我们可以得到以下几个公式:
dp[i][0]=dp[i-1][1]+dp[i-1][0];(解释一下,如果i点左右两边都没有雷那么他前一个点的情况可以是左边有雷或者两边都没有雷,下面的类似)
dp[i][1]=dp[i-1][4];
dp[i][2]=dp[i-1][0]+dp[i-1][1];
dp[i][3]=dp[i-1][4];
dp[i][4]=dp[i-1][2]+dp[i-1][3]+dp[i-1][4];
两个特殊的点,第一个点和最后一个点,第一个点只可能是自身是雷或者不是雷,那么对应dp[0][0]=dp[0][2]=1;另外第一个数左边和最后一个数与哦变不会有雷,注意细节;
AC代码:
#include<iostream>
#include<iostream>
#include<cstdio>
#include<cstring>
#define mod 1000000007
using namespace std;
char str[1000010];
int dp[1000010][7];
int main()
{
while(~scanf("%s",str+1))
{
int len=strlen(str+1),flag;
memset(dp,0,sizeof(dp));
dp[0][0]=dp[0][2]=1;
for(int i=1;i<=len;i++)
{
flag=str[i]=='?'?1:0;//为'?'则每种情况都要算上
if(flag||str[i]=='0')
{
dp[i][0]=(dp[i][0]+dp[i-1][1])%mod;
dp[i][0]=(dp[i][0]+dp[i-1][0])%mod;
}
if(flag||str[i]=='1')
{
dp[i][1]=(dp[i][1]+dp[i-1][4])%mod;
dp[i][2]=(dp[i][2]+dp[i-1][0])%mod;
dp[i][2]=(dp[i][2]+dp[i-1][1])%mod;
}
if(flag||str[i]=='2')
{
dp[i][3]=(dp[i][3]+dp[i-1][4])%mod;
}
if(flag||str[i]=='*')
{
dp[i][4]=(dp[i][4]+dp[i-1][2])%mod;
dp[i][4]=(dp[i][4]+dp[i-1][3])%mod;
dp[i][4]=(dp[i][4]+dp[i-1][4])%mod;
}
}
int ans=0;
flag=str[len]=='?'?1:0;//最后一个数不会出现右边有雷的情况(这里别忘了取模。。。)
if(flag||str[len]=='0') ans=(ans+dp[len][0])%mod;
if(flag||str[len]=='1') ans=(ans+dp[len][1])%mod;
if(flag||str[len]=='*') ans=(ans+dp[len][4])%mod;
printf("%d\n",ans);
}
return 0;
}