B. Bear and Strings

阅读: 评论:0

B. Bear and Strings

B. Bear and Strings

time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

The bear has a string s = s1ss|s| (record |s| is the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1... sj contains at least one string "bear" as a substring.

String x(i, j) contains string "bear", if there is such index k (i ≤ k ≤ j - 3), that sk = bsk + 1 = esk + 2 = ask + 3 = r.

Help the bear cope with the given problem.

Input

The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters.

Output

Print a single number — the answer to the problem.

Sample test(s) input
bearbtear
output
6
input
bearaabearc
output
20
Note

In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9).

In the second sample, the following pairs (i, j) match: (1,  4), (1,  5), (1,  6), (1,  7), (1,  8), (1,  9), (1,  10), (1,  11), (2,  10), (2,  11), (3,  10), (3,  11), (4,  10), (4,  11), (5,  10), (5,  11), (6,  10), (6,  11), (7,  10), (7,  11).


解题说明:题目的意思是找到一个包含"bear"的子串,要求bear连续出现即可,对子串的首尾没有要求。假设字符串总长为n,如果找到了bear,起始位置为i(从0开始计算),那么一共有n-i-3个子串满足包含“bear”的条件。当然,如果后面还能找到连续的“bear”,那么为了和之前的区分,从第一次出现bear的e开始到第二次出现bear的b这个区间内都可以作为第二个包含bear的子串的起始位置。这样遍历下去即可,最后累加。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include <algorithm>
#include<cstring>
using namespace std;int main()
{char s[5000];int i, j, k, n;long count = 0;char t;scanf("%s", &s);n = strlen(s);j = -1;for (i = 0; i<n - 3; i++){if (strncmp(s + i, "bear", 4) == 0){count += (i - j)*(n - i - 3);j = i;}}printf("%ldn", count);return 0;
}


本文发布于:2024-01-27 18:10:27,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063502271824.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Bear   Strings
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23