题目链接: Nun Heh Heh Aaaaaaaaaaa
给定了字符串 “nunhehheh”,简单dp
求出前缀有多少个符合的子序列,后缀有多少个a
累加 前缀的序列的个数 * 2^(后缀a的个数-1 )
累加时要注意去重,当前的序列数会包含前面一次累加时的个数,因此要保存前一次的序列数,减掉前一次的。
取模要小心
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=100019;
const int mod=998244353;
char ch[maxx];
char s[20]="#nunhehheh";
ll f[20],sum[maxx],p[maxx];
void cal()
{
sum[strlen(ch)]=0;
for(int i=strlen(ch)-1; i>=0; i--)
{
sum[i]=sum[i+1]+(ch[i]=='a');
}
}
int main()
{
// 2的n次方
p[1]=2;
for(int i=2; i<maxx; i++)
p[i]=p[i-1]*2%mod;
int t;
scanf("%d%*c",&t);
while(t--)
{
ll ans=0,pre=0;
memset(f,0,sizeof(f));
f[0]=1;
scanf("%s",ch);
cal();
int len=strlen(ch);
for(int i=0; i<len; i++)
{
for(int j=9; j>=1; j--)
f[j]=(f[j]+(ch[i]==s[j])*f[j-1])%mod;
if(ch[i]=='h'&&sum[i]&&f[9])
{
ans=(ans+(f[9]-pre+mod)%mod*((p[sum[i]]-1+mod)%mod)%mod)%mod;
pre=f[9];
}
}
printf("%lld\n",ans);
}
return 0;
}