Nun Heh Heh Aaaaaaaaaaa
作者:互联网
题目链接:Problem - 7131
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 (1≤T≤1000), 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
#include<bits/stdc++.h>
using namespace std;
const int N = 998244353;
long long dp[15][100005];
long long num[100005];
long long power(long long a, long long n){
if(n == 0){
return 1;
}
long long temp = 1;
while(n){
if(n & 1){
temp *= a;
temp %= N;
}
a *= a;
a %= N;
n /= 2;
}
return temp;
}
int main(){
ios::sync_with_stdio(false);
int k;
string s;
string t = "nunhehheh";
cin >> k;
while(k--){
cin >> s;
for(int i = 0; i < s.length() + 1; i++){
dp[0][i] = 1;
num[i] = 0;
}
for(int i = 0; i < t.length() + 1; i++){
dp[i][0] = 0;
}
dp[0][0] = 1;
for(int i = 1; i < t.length() + 1; i++){
for(int l = 1; l < s.length() + 1; l++){
dp[i][l] = dp[i][l - 1];
if(t[i - 1] == s[l - 1]){
dp[i][l] += dp[i - 1][l - 1];
dp[i][l] %= N;
}
}
}
// for(int i = 0; i < t.length() + 1; i++){
// for(int l = 0; l < s.length() + 1; l++){
// cout << dp[i][l] << " ";
// }
// cout << endl;
// }
long long ans = 0;
long long a = 0;
for(int l = s.length(); l >= 1; l--){
if(s[l - 1] == 'a'){
ans++;
}
if(s[l - 1] == 'h'){
num[l] = power(2, ans) - 1;
while(num[l] < 0){
num[l] += N;
}
}
}
long long sum = 0;
for(int i = 1; i < s.length() + 1; i ++){
if(s[i - 1] == 'h'){
long long cnt = num[i] * ((dp[9][i] - a + N) % N);
cnt %= N;
sum += cnt;
sum %= N;
a = dp[9][i];
}
}
cout << sum << endl;
}
return 0;
}
//n u n h e h h e h a h a a h a h a h a h a h a h a a h a a h a h a h a h h a
标签:string,int,long,Nun,++,length,Aaaaaaaaaaa,Heh,dp 来源: https://blog.csdn.net/m0_55682843/article/details/120701586