• Codeforces 385B. Bear and Strings


    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 = 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.

    暴力加上少量的优化

    考虑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

    #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] = '';
          if (strstr(s+j-3,tar) != NULL){
            total += len - j;
            s[j+1] = temp;
            break;
          }
          s[j+1] = temp;
        }
      }
      printf("%d
    ",total);
      return 0;
    }
    View Code
  • 相关阅读:
    HTML初步学习7
    HTML初步学习6
    HTML初步学习5
    HTML初步学习4
    poj3449Geometric Shapes
    poj2074Line of Sight(直线相交)
    2014 Multi-University Training Contest 4
    poj3347Kadj Squares
    poj1556The Doors
    poj3608Bridge Across Islands(凸包间最小距离)
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7446292.html
Copyright © 2020-2023  润新知