[[20190614]] JZOJ6216 sequence count

topic

A length \ (N \) strings \ (S \) , \ (M \) interrogation interval \ ([l, r] \ ) a different number of sub-strings, the character set is $ C $

\ (N, M \ 10 ^ 5 \, \ C \ 10 \)

answer

  • This problem is very routine. . .

  • part 1

  • Set \ (dp_ {i, j} \) to account i, the number of substrings, the end character j, consider \ (S_i = C \)
    \ [\ the begin {align = left} & dp_ {i, J} = dp_ {i, j} \\ & dp_ {i, c} = \ sum_ {j} dp_ {i-1, j} + 1 \ end {align} \]

  • part 2

  • Set a size \ (C + 1 \) matrix, write transfer matrix
    \ [A_i = \ begin {pmatrix } 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 1 & 1 & 1 & 1 \\ 0 & 0 & 0 &
    1 \\ \ end {pmatrix} \] its inverse matrix
    \ [B_i = \ begin {pmatrix } 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ -1 & -1 & -1 & -1 \\
    0 & 0 & 0 & 1 \\ \ end {pmatrix} \] only need to prefix the preprocessing matrix

  • part 3

    Take a left A row becomes the equivalent of a line and all, and can maintain a

    Take a right equivalent to a B minus a column other columns, you can maintain the overall subtraction mark

    Statistics can also answer directly thing to maintain, in particular, see the code

    Time complexity $ O ((N + M) C) $

#include<bits/stdc++.h>
using namespace std;
const int N=500010,mod=1e9+7;
int n,m,f1[N][11],f2[N][11];

char s[N],ps[1000000],*pp=ps;
void flush(){fwrite(ps,1,pp-ps,stdout);pp=ps;}
void push(char x){if(pp==ps+1000000)flush();*pp++=x;}
void write(int x){
    static int sta[20],top;
    if(!x){push('0');push('\n');return;}
    while(x)sta[++top]=x%10,x/=10;
    while(top)push(sta[top--]^'0');
    push('\n');
}

void inc(int&x,int y){x+=y;if(x>=mod)x-=mod;}
void dec(int&x,int y){x-=y;if(x<0)x+=mod;}

struct Mat{
    int c[11][11],s[11],d[11],D;
    void init(){for(int i=0;i<11;++i)c[i][i]=s[i]=1;}
    void plus(int I){
        for(int i=0;i<11;++i){
            int t=c[I][i];c[I][i]=s[i];
            s[i]=(2*s[i]%mod-t+mod)%mod;
        }
    }
    void minus(int I){
        for(int i=0;i<11;++i){
            int t=d[i];d[i]=c[i][I];
            c[i][I]=(2*d[i]%mod-t+mod)%mod;
        }
    }
    void print(int typ){
        for(int i=0;i<11;++i,puts(""))
        for(int j=0;j<11;++j){
            int t=typ?(c[i][j]-d[i]+mod)%mod:c[i][j];
            if(mod-t<t)t=t-mod;
            printf("% 2d ",t);
        }
        puts("\n");
        if(!typ)for(int i=0;i<11;++i)printf("%d ",s[i]);
        puts("\n");
    }
}A,B;

int main(){
    freopen("sequence.in","r",stdin);
    freopen("sequence.out","w",stdout);
    scanf("%s%d",s+1,&m);n=strlen(s+1);
    A.init(),B.init();f2[0][10]=1;
    for(int i=1;i<=n;++i){
        A.plus(s[i]-'a');
        B.minus(s[i]-'a');
        for(int j=0;j<11;++j){
            f1[i][j]=A.s[j];
            f2[i][j]=(B.c[j][10]-B.d[j]+mod)%mod;
        }
    }
    for(int i=1,l,r,ans;i<=m;++i){
        scanf("%d%d",&l,&r);
        ans=0;for(int j=0;j<11;++j)inc(ans,1ll*f1[r][j]*f2[l-1][j]%mod);
        if(!ans)ans=mod-1;else ans--;write(ans);
    }
    return flush(),0;
}

Guess you like

Origin www.cnblogs.com/Paul-Guderian/p/11074480.html