zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Linux下目录的权限详解
    Linux下mknod详解
    迅雷精简版-无广告
    PotPlayer一款简洁好用的播放器
    Linux 信号signal处理机制
    Kruskal算法求最小生成树
    Prim算法求权数和,POJ(1258)
    Prim算法求最大权,POJ(2485)
    Prim算法
    DP找最优配置,(POJ1018)
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7446292.html
Copyright © 2011-2022 走看看