2021中国大学生程序设计竞赛(CCPC)- 网络选拔赛(重赛)

Nun Heh Heh Aaaaaaaaaaa

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)

Problem Description

Vasily Tadokorov is a stringologist. He thinks a string is fragrant if it can be divided into two parts — nunhehheh as the prefix and a number of (excluding 0) a as the suffix. For example, nunhehhehaaaaaa is fragrant, but nunhehheh and nunhehhehoooaaa are not fragrant.

Today Vasily Tadokorov has some strings consisting of lowercase English letters. For each string, he wants to know how many subsequences of this string are fragrant. A string a is a subsequence of a string b if a can be obtained from b by deletion of several (including 0) characters.

Input

The first line contains an integer T(1T1000), denoting the number of strings.

Each of the next T lines contains a string S(1|S|105) consisting of lowercase English letters.

The total length of the strings in the input will not exceed 106.

Output

For each of the given T strings, output the answer modulo 998244353.

Sample Input

2 nunhehhehahaahahahahahahaahaahahahahha nunhehhehhehhahaahahahaahaahahaaaahaa

Sample Output

114514 1919810

解题思路

dp

先考虑这样一问题:子序列 p 在母串 s 中出现的次数?

  • 状态表示:f(i,j) 表示子序列的前 j 个字符在母串 s 的前 i 个字符出现的次数
  • 状态计算:
    • f[i][1]=f[i1][1]+(s[i]==p[1])
    • f[i][j]=f[i1][j]+(s[i]==p[j])f[i1][j1]
      分析:f[i][1] 容易得到转移方程;j>1 时,当 s[i]p[j],显然,f[i][j]=f[i1][j],否则以子序列的第 j 个字符作为分界点,可分为两种情况:1.子序列的前 j 个字符在母串的前 i1 出现的次数,即不考虑母串的第 i 个字符造成的影响。2.考虑母串的第 i 个字符造成的影响时,等价于子序列的前 j1 个字符在母串的前 i1 出现的次数

每次维护答案增加的次数,即每两个a之间子序列nunhehheh出现的次数乘以后面a数量的方案数

  • 时间复杂度:O(9×n×T)

代码

#include<bits/stdc++.h> using namespace std; const int mod=998244353; string p=" nunhehheh"; long long f[12]; int mx[100005]; char s[100005]; long long ksm(int a,int b) { long long res=1%mod; for(;b;b>>=1) { if(b&1)res=(res*a)%mod; a=1ll*a*a%mod; } return res; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%s",s+1); int len=strlen(s+1); long long res=0; memset(f,0,sizeof f); memset(mx,0,sizeof mx); long long last=0; for(int i=len;i>=1;i--) mx[i]=mx[i+1]+(s[i]=='a'); for(int i=1;i<=len;i++) { for(int j=9;j>1;j--) { f[j]=(f[j]+(s[i]==p[j])*f[j-1])%mod; } f[1]=(f[1]+(s[i]==p[1]))%mod; if(s[i]=='a')res=(res+(f[9]-last+mod)%mod*(ksm(2,mx[i])-1+mod)%mod)%mod,last=f[9]%mod; } printf("%lld\n",res); } return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/15390545.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(726)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示