cf div2 237 D
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.
DP 给5种情况进行编号,0代表当前位置是0,1代表当前位置是1且左边有炸弹,2代表当前位置是1左边没有炸弹,3代表当前位置是2,4代表当前位置是炸弹
dp2[0] = dp1[0];
dp2[1] = dp1[4];
dp2[2] = dp1[0] + dp1[1] ;
dp2[3] = dp1[4];
dp2[4] = dp1[4] + dp1[2] + dp1[3];
注意边界和数据范围即可。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 8 typedef long long ll; 9 10 #define maxn 1000005 11 #define MOD 1000000007 12 13 char s[maxn]; 14 int dp[2][5]; 15 void solve() { 16 17 int *dp1 = dp[0],*dp2 = dp[1]; 18 dp1[2] = strlen(s) != 1 && (s[0] == '1' || s[0] == '?'); 19 dp1[0] = s[0] == '0' || s[0] == '?'; 20 dp1[4] = s[0] == '*' || s[0] == '?'; 21 for(int i = 1; i < strlen(s); ++i) { 22 if(s[i] == '?' || s[i] == '1') { 23 if(i != strlen(s) - 1) 24 dp2[2] = (dp1[0] + dp1[1]) % MOD; 25 dp2[1] = dp1[4]; 26 } 27 if(s[i] == '?' || s[i] == '2') { 28 if(i != strlen(s) - 1) 29 dp2[3] = dp1[4]; 30 } 31 if(s[i] == '?' || s[i] == '0') { 32 dp2[0] = (dp1[0] + dp1[1]) % MOD; 33 } 34 if(s[i] == '?' || s[i] == '*') { 35 36 dp2[4] = ((ll)dp1[3] + dp1[2] + dp1[4]) % MOD; 37 } 38 39 40 swap(dp1,dp2); 41 for(int j = 0; j <= 4; ++j) dp2[j] = 0; 42 } 43 44 int ans = 0; 45 for(int i = 0; i <= 4; ++i) { 46 47 ans = (ans + dp1[i]) % MOD; 48 } 49 50 printf("%d\n",ans); 51 } 52 53 int main() { 54 //freopen("sw.in","r",stdin); 55 56 scanf("%s",s); 57 58 solve(); 59 60 return 0; 61 }