Codeforces Round #226 (Div. 2)B. Bear and Strings
来源:互联网 发布:昂达主板怎么样知乎 编辑:程序博客网 时间:2024/05/05 05:52
The bear has a string s = s1s2... s|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 = b, sk + 1 = e, sk + 2 = a, sk + 3 = r.
Help the bear cope with the given problem.
The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters.
Print a single number — the answer to the problem.
bearbtear
6
bearaabearc
20
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时,先计算后面有多少组,即为(len-(i+3))组,在加上前面的组数,b为上一个b字符对应的坐标(开始为-1),则前面有(i-b-1)*(len-i-3);代码如下:
#include<stdio.h>#include<string.h>char s[5010];int main(){ int i,len,sum,b; scanf("%s",s); len=strlen(s); sum=0;b=-1; for(i=0;i<len;i++){ if(s[i]=='b'){ if(s[i+1]=='e'&&s[i+2]=='a'&&s[i+3]=='r'){ sum+=len-i-3+(i-b-1)*(len-i-3); b=i; } } } printf("%d\n",sum); return 0;}
- Codeforces Round #226 (Div. 2) B. Bear and Strings
- Codeforces Round #226 (Div. 2)B. Bear and Strings
- Codeforces Round #226 (Div. 2)B. Bear and Strings
- Codeforces Round #356 (Div. 2) B Bear and Finding Criminals
- Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings
- Codeforces Round #226 (Div. 2) A. Bear and Raspberry
- Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers
- Codeforces Round #226 (Div. 2)A. Bear and Raspberry
- Codeforces Round #226 (Div. 2)A. Bear and Raspberry
- Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
- Codeforces Round #226 (Div. 2) D. Bear and Floodlight
- Codeforces Round #226 (Div. 2)--A Bear and Raspberry
- Codeforces Round #226 (Div. 2)--C. Bear and Prime Numbers
- Codeforces Round #405 Div. 1 B. Bear and Tree Jumps
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)B Bear and Three Musketeers
- Codeforces Round #179 (Div. 2) B (codeforces 296b) Yaroslav and Two Strings
- Codeforces 385B Bear and Strings
- 郭晶晶成功瘦身中环逛街 产后专心相夫教子
- 情感交互—移动应用产品交互趋势
- 我的商业秘密
- 各种常用的JSON接口
- linux 挂载U盘
- Codeforces Round #226 (Div. 2)B. Bear and Strings
- ExtJS做的一个信息管理界面,项目代码
- App盈利四趋势:硬件、垂直、企业、平民
- 【C/C++】内存分配函数:malloc,calloc,realloc,_alloca
- 全国域名解析首遭大规模污染
- 回到过去:8位成功创业者希望年轻的自己能领悟的道理 | 从Pandora的创始人Tim Westergren到维基百科的Jimmy Wales, 我们以为这些成功故事的主角们应该是年纪轻轻时便已成竹在
- printk 函数
- 解决extjs grid 不随窗口大小自适应的问题
- robot-underground源码分析(一)