[USACO23OPEN] Pareidolia S
USACO23OPEN] Pareidolia S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
目录题目背景
Note: The time limit for this problem is 4s, 2x the default.
Pareidolia is the phenomenon where your eyes tend to see familiar patterns in
images where none really exist -- for example seeing a face in a cloud. As you
might imagine, with Farmer John's constant proximity to cows, he often sees
cow-related patterns in everyday objects. For example, if he looks at the
string "bqessiyexbesszieb", Farmer John's eyes ignore some of the letters and
all he sees is "bessiebessie".
题面翻译
Farmer John有的时候看东西会忽略一些字母,如把 bqessiyexbesszieb
看成 bessiebessie
。定义 \(B(s)\) 表示把 \(s\) 中的若干个字母删去后,形成尽量多个 bessie
相连的形式 (bessiebessiebessieb...
),返回 bessie
的最大数量。如 \(B(bqessiyexbesszieb)=2\)。对字符串 \(s\) 计算 \(B(s)\) 是一个有趣的挑战,但FJ想到了一个更有趣的挑战:对 \(s\) 的所有子串进行计算B函数,并求和。\(s\) 的长度不超过 \(3*10^5\)。
题目描述
Given a string \(s\), let \(B(s)\) represent the maximum number of repeated copies
of "bessie" one can form by deleting zero or more of the characters from \(s\).
In the example above, \(B(\text{``bqessiyexbesszieb"}) = 2\).
Computing \(B(s)\) is an interesting challenge, but Farmer John is interested in
solving a challenge that is even more interesting: Given a string \(t\) of length
at most \(3\cdot 10^5\) consisting only of characters a-z, compute the sum of
\(B(s)\) over all contiguous substrings \(s\) of \(t\).
输入格式
The input consists of a nonempty string of length at most \(3\cdot 10^5\) whose
characters are all lowercase English letters.
输出格式
Output a single number, the total number of bessies that can be made across all
substrings of the input string.
样例 #1
样例输入 #1
bessiebessie
样例输出 #1
14
样例 #2
样例输入 #2
abcdefghssijebessie
样例输出 #2
28
提示
For the first sample, twelve substrings contain exactly \(1\) "bessie", and \(1\) string contains exactly \(2\) "bessie"s, so the total is \(12\cdot 1+1\cdot 2=14\).
- Inputs 3-5: The string has length at most \(5000\).
- Inputs 6-12: No additional constraints.
分析
考试时想到了假的 \(O(N^2)\) 的做法,感觉好像跟正解差不多,但就是改不出来了
设 \(dp[i]\) 表示前 \(i\) 个数里面有多少个目标字符串
设最后一个字符串的开头位于 \(j\)
则
\[dp[i] = dp[j - 1] + j \]答案为
\[\sum_{i = 1}^ndp[i] \]再维护一下开头的位置就好了,不会的可以看一下代码
code
#include <bits/stdc++.h>
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
using namespace std;
const int N = 3e5 + 5;
int len , a[N];
long long f[10] , ans , dp[N];
char s[N];
int main () {
scanf ("%s" , s + 1);
len = strlen (s + 1);
// bessie
// 123342
// 123645
fu (i , 1 , len) {
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];
}
fu (i , 1 , len)
ans += dp[i];
printf ("%lld" , ans);
return 0;
}
标签:USACO23OPEN,string,cdot,样例,bessie,Pareidolia
From: https://www.cnblogs.com/2020fengziyang/p/17555903.html