Codeforces 385B

Bear and Strings

Problem Description

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.

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 Input

bearbtear

Sample Output

6

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{

int a[5010];
char s[5010];
int i,now,sum,len,j,flag;
while(scanf("%s",s)!=EOF)
{
len=strlen(s);
now=1;
sum=0;
memset(a,0,sizeof(a));
for(i=3;i<len;i++)
{
if(s[i-3]=='b'&&s[i-2]=='e'&&s[i-1]=='a'&&s[i]=='r')
{
a[i]=1;
}
}
for(i=0;i<len;i++)
{
flag=-1;
for(j=i;j<len-3;j++)
{
if(s[j]=='b'&&s[j+1]=='e'&&s[j+2]=='a'&&s[j+3]=='r')
{
flag=j;
break;
}
}

if(flag!=-1)
{
//printf("%c\n",s[i]);
sum+=(len-flag-3);
//printf("sum+=len-3 %d %d\n",flag,len-flag-3);
}
}
printf("%d\n",sum);
}
return 0;
}