cf404D Minesweeper 1D 状态转移dp

来源:互联网 发布:官方软件 编辑:程序博客网 时间:2024/06/09 20:59

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 isn 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, field2* 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 ofn 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 thei-th cell of the field contains a bomb. If charactersi equals "?", then Valera hasn't yet decided what to put in thei-th cell. Character si, that is equal to a digit, represents the digit written in thei-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).



#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <bitset>using namespace std ;#define zero {0}#define INF 2000000000#define EPS 1e-6typedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}const int mod = 1000000007;long long dp[1000005][5] = zero;char s[1000005];/*0 这位置为01 这位置为1且左边是雷2 这位置为1且右边是雷3 这位置为24 这位置为雷*/int main(){#ifdef DeBUGs    freopen("//home//amb//桌面//1.in", "r", stdin);#endif    while (scanf("%s", s) + 1)    {        memset(dp, 0, sizeof(dp));        int l = strlen(s);        if (s[0] == '0')        {            dp[0][0] = 1;        }        else if (s[0] == '1')        {            dp[0][2] = 1;        }        else if (s[0] == '2')        {            printf("0\n");            continue;        }        else if (s[0] == '*')        {            dp[0][4] = 1;        }        else            dp[0][0] = dp[0][2] = dp[0][4] = 1;        for (int i = 1; i < l; i++)        {            if (s[i] == '0' || s[i] == '?')            {                dp[i][0] = (dp[i - 1][1] + dp[i - 1][0]) % mod;            }            if (s[i] == '1' || s[i] == '?')            {                dp[i][1] = dp[i - 1][4];                dp[i][2] = (dp[i - 1][0] + dp[i - 1][1]) % mod;            }            if (s[i] == '2' || s[i] == '?')            {                dp[i][3] = dp[i - 1][4];            }            if (s[i] == '*' || s[i] == '?')            {                dp[i][4] = (dp[i - 1][2] + dp[i - 1][3] + dp[i - 1][4]) % mod;            }        }        // for (int i = 0; i <l; i++)        // {        //     printf("%d%d%d%d%d\n", dp[i][0], dp[i][1], dp[i][2], dp[i][3],dp[i][4]);        // }        printf("%I64d\n", (dp[l - 1][0] + dp[l - 1][1] + dp[l - 1][4]) % mod);    }    return 0;}// ?01???// ?// **12// 1// ************// 10101// 10000// 00100// 00001// 01011// 11112// 4// ************// 10101// 2// ************// 00001// 00001// 01000// 00000// 0// ************// 00100// 0


0 0
原创粉丝点击