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

 /*
  题意就是要找到包含“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 }

 

posted @ 2014-03-12 14:27  Roly Yu  阅读(232)  评论(0编辑  收藏  举报