题解

cookiebus 2023-10-19 22:29:58 19 返回题目

soure:P9188

首先考虑 B 函数怎么求,只需要对这个序列从左往右扫,不断匹配 bessie 这个子串,最后统计有匹配出几个即可。

考虑根据这个匹配过程 dp,设 dp[i][j] 表示到了第 i 个位置,匹配到第 j 个字母的左端点个数,则当匹配

完一个 bessie 的时候,就可以算贡献,这个贡献对于每个右端点都成立。时间复杂度 O(n)

#include<bits/stdc++.h>
#define Gc() getchar() 
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define PB push_back
#define fi first
#define se second
using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;
using namespace std;const int N=3e5+5,M=5e6+5,K=3e7+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(time(0));
int n,m,k,x,y,z;ll f[6],Ans;char c[N];
int main(){
    freopen("bessie.in","r",stdin);freopen("bessie.out","w",stdout);
    int i,j;cin>>c+1;n=strlen(c+1);
    for(i=1;i<=n;i++){
        f[0]++;
        if(c[i]=='b') f[1]+=f[0],f[0]=0;
        else if(c[i]=='e') f[2]+=f[1],f[1]=0,f[0]+=f[5],Ans+=f[5]*(n-i+1),f[5]=0;
        else if(c[i]=='s') f[4]+=f[3],f[3]=f[2],f[2]=0;
        else if(c[i]=='i') f[5]+=f[4],f[4]=0;
        // cerr<<f[0]<<' '<<f[1]<<' '<<f[2]<<' '<<f[3]<<' '<<f[4]<<' '<<f[5]<<' '<<Ans<<'\n';
    }
    printf("%lld\n",Ans);
}

{{ vote && vote.total.up }}