[USACO23OPEN] Pareidolia S

[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 的长度不超过 3105

题目描述

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(``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 3105 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 3105 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 121+12=14.

  • Inputs 3-5: The string has length at most 5000.
  • Inputs 6-12: No additional constraints.

分析

考试时想到了假的 O(N2) 的做法,感觉好像跟正解差不多,但就是改不出来了

dp[i] 表示前 i 个数里面有多少个目标字符串

设最后一个字符串的开头位于 j

dp[i]=dp[j1]+j

答案为

i=1ndp[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;
}
posted @   2020fengziyang  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示