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.
暴力加上少量的优化
考虑s(i,j),如果s(i,j)包含bear,易得s(i,r)(r>=j)肯定都包含bear,所以直接加上后面的数量
如果s(i,j)不包含bear,那么s(i+1,j)如果有bear,肯定就是最后四个字符是bear,所以实际我们搜索只有去看s(j-3,j)这一部分就OK
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; typedef long long LL; // const int SIZE=1e6+7; char s[5005]; const char* tar = "bear"; int total = 0; int main(){ // freopen("test.in","r",stdin); scanf("%s",s); int len = strlen(s); for (int i=0;i<len;i++){ for (int j=i+3;j<len;j++){ char temp = s[j+1]; s[j+1] = '