P9188题解
Nuyoah_awa · · 题解
题目大意
给定一个字符串,求这个字符串所以子段中 bessie 出现的数量。
题目分析
思路:dp。
我们观察一个子段的值是子段中 bessie 的数量,我们可以考虑把这个值分开来算。
我们定义 bessie 的数量。我们从第 bessie,第一个 bessie 的 b 在第
转移方程为:
答案为:
其中 bessie,在末尾拼上 bessie 在某个子段中出现几次就会被记几次。
然后问题就变成了如何求 bessie 的前 b 出现的位置,转移方程为:如果当前字符串为 bessie 的第 bessie 中的 b 出现的位置为
这样时间复杂度就是
code
#include <iostream>
#include <cstdio>
#define int long long
using namespace std;
const int N = 3e5 + 5;
int n, f[7], ans, dp[N];
string s;
signed main()
{
cin >> s;
n = s.size();
s = "#" + s;
for(int i = 1;i <= n;i++)
{
if(s[i] == 'b')
f[1] = i;
if(s[i] == 'e')
f[6] = f[5], f[2] = f[1];
if(s[i] == 's')
f[4] = f[3], f[3] = f[2];
if(s[i] == 'i')
f[5] = f[4];
dp[i] = dp[f[6] - 1] + f[6];
}
for(int i = 1;i <= n;i++)
ans += dp[i];
printf("%lld", ans);
return 0;
}