老年选手的复健之路
CF1426F Number of Subsequences
Description
一个字符串中包含\(a,b,c,?\) 这4种字符,\(?\)可变成\(a,b,c\)
求所有可能的串中有多少个子串是\(abc\)
Code
展开
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int N = 2e5 + 10; const int mod = 1e9 + 7; int n; ll ans, Fa[N], Fb[N], Fc[N]; char ch[N]; int main() { scanf("%d", &n); scanf("%s", ch + 1); ll pow_3 = 1; for (int i = 1; i <= n; ++i) { if (ch[i] == 'a') { Fa[i] = (Fa[i - 1] + pow_3) % mod; Fb[i] = Fb[i - 1]; Fc[i] = Fc[i - 1]; } if (ch[i] == 'b') { Fa[i] = Fa[i - 1]; Fb[i] = (Fb[i - 1] + Fa[i - 1]) % mod; Fc[i] = Fc[i - 1]; } if (ch[i] == 'c') { Fa[i] = Fa[i - 1]; Fb[i] = Fb[i - 1]; Fc[i] = (Fc[i - 1] + Fb[i - 1]) % mod; } if (ch[i] == '?') { Fa[i] = Fa[i - 1] * 3 % mod; Fb[i] = Fb[i - 1] * 3 % mod; Fc[i] = Fc[i - 1] * 3 % mod; Fa[i] = (Fa[i] + pow_3) % mod; Fb[i] = (Fb[i] + Fa[i - 1]) % mod; Fc[i] = (Fc[i] + Fb[i - 1]) % mod; pow_3 = pow_3 * 3 % mod; } } printf("%lld\n", Fc[n]); }