mine:dp

A small linear dp. Many ways, Eight Immortals recount.

I want to talk about me:

. 1 #include <cstdio>
 2  #define MOD 1,000,000,007
 . 3  char S [ 1000005 ]; int DP [ 1000005 ] [ 2 ] [ 2 ], n-; // is not mine, there is no right of mine 
. 4  int main () {
 . 5      Scanf ( " % S " , S + . 1 );
 . 6      for (n-= . 1 ; S [n-]; n-++); N-- ;
 . 7      DP [ 0 ] [ 0 ] [ 0 ] = DP [ 0 ] [ 0 ] [ . 1 ] =1;
 8     for(int i=1;i<=n;++i)
 9         if(s[i]=='0')dp[i][0][0]=dp[i-1][0][0];
10         else if(s[i]=='1')dp[i][0][0]=dp[i-1][1][0],dp[i][0][1]=dp[i-1][0][0];
11         else if(s[i]=='2')dp[i][0][1]=dp[i-1][1][0];
12         else if(s[i]=='*')dp[i][1][1]=dp[i][1][0]=(dp[i-1][0][1]+dp[i-1][1][1])%mod;
13         else {
14             dp[i][0][0]=dp[i-1][0][0];//0
15             (dp[i][0][0]+=dp[i-1][1][0])%=mod;dp[i][0][1]=dp[i-1][0][0];//1
16             (dp[i][0][1]+=dp[i-1][1][0])%=mod;//2
17             dp[i][1][1]=dp[i][1][0]=(dp[i-1][0][1]+dp[i-1][1][1])%mod;//*
18         }
19     printf("%d\n",(dp[n][0][0]+dp[n][1][0])%mod);
20 }
To throw in here

My meaning is dp: dp [i] [this] [next] represents the i-th position after consideration, there is no program number and mine this next position.

Consider: If you know what the previous one is what this one is, you can deduce what the next one yes.

Because there are constraints in the one-dimensional role between demining, only three adjacent grid.

Therefore, we consider dp [i] [t] [n] by what transferred from:

If bit i is '0', then it is not mine , it is in front of a not Ray , hereinafter also not be , the DP [i] [ 0 ] [ 0 ] = DP [i-. 1] [ 0 ] [ 0 ] (Note that the character codes corresponding to the color)

If it is a '1', then it's not mine , divided into two kinds:

It front and rear No : DP [I] [ 0 ] [ 0 ] DP = [. 1-I] [ . 1 ] [ 0 ];

It is not preceded and followed by : DP [I] [ 0 ] [ . 1 ] DP = [-I. 1] [ 0 ] [ 0 ];

If it is '2', then it is not mine , but before post has Ray: DP [I] [ 0 ] [ . 1 ] DP = [. 1-I] [ . 1 ] [ 0 ];

If it is a mine , then freely in front. . The back is also random. . : DP [I] [ . 1 ] [. 1] DP = [I] [ . 1 ] [0] DP = [-I. 1] [0] [ . 1 ] + DP [-I. 1] [. 1] [ . 1 ];

If you put a question mark over the top of these states Dunong like.

First state dp [0] [0] [0] = dp [0] [0] [1] = 1 because there is no mine and on the first lattice 0 has no right to say it will not be restricted 0 lattice.

End state dp [n] [1] [0] + dp [n] [0] [0] because there are no mine and care on the n-th lattice (if it is not legal then dp 0), and the n +1 on the grid is not mine.

Finished!

 

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/11364776.html