Codeforces Round #226 (Div. 2)B. Bear and Strings

来源:互联网 发布:淘宝店招设计素材 编辑:程序博客网 时间:2024/05/14 08:15
 /*
  题意就是要找到包含“bear”的子串,计算出个数,需要注意的地方就是不要计算重复。
*/
1
#include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #define maxn 5005 5 6 char str[maxn]; 7 int pos[maxn]; 8 int main() 9 {10 while(~scanf("%s",str))11 {12 int p = 1;13 memset(pos, 0, sizeof(int));14 int len = (int)strlen(str);15 for(int i = 0;i < len;i++){16 if(str[i] == 'b' && str[i+1] == 'e' && str[i+2] == 'a' && str[i+3] == 'r')17 pos[p++] = i;18 }19 // for(int i = 0;i < p;i++)20 // printf("%d ",pos[i]);21 int ans = 0;22 int num;23 pos[0] = -1;24 for(int i = 1;i < p;i++){25 num = (len - pos[i] - 3)*(pos[i]-pos[i-1]);26 //printf("%d*\n",num);27 ans += num;28 }29 printf("%d\n",ans);30 }31 return 0;32 }
0 0
原创粉丝点击