[USACO23OPEN] Pareidolia S

[USACO23OPEN] Pareidolia S

USACO23OPEN] Pareidolia S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

文章目录

  • [USACO23OPEN] Pareidolia S
    • 题目背景
    • 题面翻译
    • 题目描述
    • 输入格式
    • 输出格式
    • 样例 #1
      • 样例输入 #1
      • 样例输出 #1
    • 样例 #2
      • 样例输入 #2
      • 样例输出 #2
    • 提示
    • 分析
    • code

题目背景

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 ) B(s) B(s) 表示把 s s s 中的若干个字母删去后,形成尽量多个 bessie 相连的形式 (bessiebessiebessieb...),返回 bessie 的最大数量。如 B ( b q e s s i y e x b e s s z i e b ) = 2 B(bqessiyexbesszieb)=2 B(bqessiyexbesszieb)=2。对字符串 s s s 计算 B ( s ) B(s) B(s) 是一个有趣的挑战,但FJ想到了一个更有趣的挑战:对 s s s 的所有子串进行计算B函数,并求和。 s s s 的长度不超过 3 ∗ 1 0 5 3*10^5 3105

题目描述

Given a string s s s, let B ( s ) B(s) B(s) represent the maximum number of repeated copies
of “bessie” one can form by deleting zero or more of the characters from s s s.
In the example above, B ( “bqessiyexbesszieb" ) = 2 B(\text{``bqessiyexbesszieb"}) = 2 B(“bqessiyexbesszieb")=2.

Computing B ( s ) B(s) B(s) is an interesting challenge, but Farmer John is interested in
solving a challenge that is even more interesting: Given a string t t t of length
at most 3 ⋅ 1 0 5 3\cdot 10^5 3105 consisting only of characters a-z, compute the sum of
B ( s ) B(s) B(s) over all contiguous substrings s s s of t t t.

输入格式

The input consists of a nonempty string of length at most 3 ⋅ 1 0 5 3\cdot 10^5 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 1 1 “bessie”, and 1 1 1 string contains exactly 2 2 2 "bessie"s, so the total is 12 ⋅ 1 + 1 ⋅ 2 = 14 12\cdot 1+1\cdot 2=14 121+12=14.

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

分析

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

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

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


d p [ i ] = d p [ j − 1 ] + j dp[i] = dp[j - 1] + j dp[i]=dp[j1]+j
答案为
∑ i = 1 n d p [ i ] \sum_{i = 1}^ndp[i] i=1ndp[i]
再维护一下开头的位置就好了,不会的可以看一下代码

code

#include 
#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;
}

你可能感兴趣的:(dp,算法)