White cattle off season May 19 J- "Fire" Royal flames relating to the state before and after the four-dimensional dp dimensionality reduction

Topic link: https: //ac.nowcoder.com/acm/contest/2272/J
subject to the effect:
Here Insert Picture Description
My thinking is f [i] [j] [ k] [w]: representation before i lattice
j: front there is no a lattice
k: the grid has no
next grid there: w
number of cases.

State represents a redundancy. We use the f [i] [j] [ k] represents the lattice before i
j: This grid has no
k: there is no next grid
on it.

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int mod=1e9+7;

char s[1000005];
LL f[1000005][2][2]={0};
int main()
{
    scanf("%s", s+1);
    int n=strlen(s+1);

    f[0][0][0]=1;
    f[0][0][1]=1;
    for(int i=1; i<=n; i++){
        if(s[i]=='0'){
            f[i][0][0]+=f[i-1][0][0];
            f[i][0][0]%=mod;
        }
        if(s[i]=='1'){
            f[i][0][0]+=f[i-1][1][0];
            f[i][0][1]+=f[i-1][0][0];
            f[i][0][0]%=mod;
            f[i][0][1]%=mod;
        }
        if(s[i]=='2'){
            f[i][0][1]+=f[i-1][1][0];
            f[i][0][1]%=mod;
        }
        if(s[i]=='*'){
            f[i][1][0]+=(f[i-1][1][1]+f[i-1][0][1]);
            f[i][1][1]=f[i][1][0];
            f[i][1][0]%=mod;
            f[i][1][1]%=mod;
        }
        if(s[i]=='?'){
            f[i][1][0]+=(f[i-1][1][1]+f[i-1][0][1]);
            f[i][1][1]+=f[i][1][0];
            f[i][0][0]+=(f[i-1][1][0]+f[i-1][0][0]);
            f[i][0][1]+=f[i][0][0];
            f[i][1][0]%=mod;
            f[i][1][1]%=mod;
            f[i][0][0]%=mod;
            f[i][1][0]%=mod;
        }
    }
    printf("%lld\n", (f[n][0][0]+f[n][1][0])%mod);

    return 0;
}
Published 374 original articles · won praise 22 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_21433411/article/details/103225598