Nun Heh Heh Aaaaaaaaaaa
Link
题意
nunhehheh+a(一系列的a)是合法的,问一个字符串有多少个合法的子串。
题解
考虑如何不重不漏的划分所有的方案?我们可以维护一个 f [ i ] [ j ] : 前 i 个 字 母 中 以 n u n h e h h e h 中 第 j 个 结 尾 的 数 目 f[i][j]:前i个字母中以nunhehheh中第j个结尾的数目 f[i][j]:前i个字母中以nunhehheh中第j个结尾的数目,那么假设第 i i i个是 h h h那么我们只需要找到前 i − 1 个 里 以 n u n h e h h e h 的 第 8 个 结 尾 的 序 列 的 数 目 i - 1个里以nunhehheh的第8个结尾的序列的数目 i−1个里以nunhehheh的第8个结尾的序列的数目,然后将第i个分别接到他们的后面,就是一个 n u n h e h h e h nunhehheh nunhehheh。然后对于第 i i i个后面的所有的 a a a每一个选与不选一共有 2 n 2^n 2n中选择,因为不能是空集,所以一共 2 n − 1 2^n -1 2n−1种选择,将他们相乘就是位置i的贡献,从前往后加一下每一个点的贡献就是所有的方案数。
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 998244353;
const double eps = 1e-8;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int f[N][10];
int sum[N];
LL qmi(LL a, LL b, LL p) {
LL res = 1;
while (b) {
if (b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while (T -- ) {
string str;
cin >> str;
str = " " + str;
sum[str.size()] = 0;
//Nun Heh Heh
for (int i = str.size() - 1; i; i --) sum[i] = sum[i + 1] + (str[i] == 'a');
LL res = 0;
for (int i = 1; i < str.size(); i ++ ) {
f[i][1] = (f[i - 1][1] + (str[i] == 'n')) % mod;
f[i][2] = (f[i - 1][2] + f[i - 1][1] * (str[i] == 'u')) % mod;
f[i][3] = (f[i - 1][3] + f[i - 1][2] * (str[i] == 'n')) % mod;
f[i][4] = (f[i - 1][4] + f[i - 1][3] * (str[i] == 'h')) % mod;
f[i][5] = (f[i - 1][5] + f[i - 1][4] * (str[i] == 'e')) % mod;
f[i][6] = (f[i - 1][6] + f[i - 1][5] * (str[i] == 'h')) % mod;
f[i][7] = (f[i - 1][7] + f[i - 1][6] * (str[i] == 'h')) % mod;
f[i][8] = (f[i - 1][8] + f[i - 1][7] * (str[i] == 'e')) % mod;
f[i][9] = (f[i - 1][8] * (str[i] == 'h')) % mod;
if (f[i][9]) res = (res + f[i][9] * ((qmi(2, sum[i + 1], mod)) - 1)) % mod;
res %= mod;
}
cout << res << endl;
}
return 0;
}