Codeforces Round #226 (Div. 2), problem: (B) Bear and Strings editorial: http://codeforces.com/blog/entry/10514
Codeforces Round #226 (Div. 2), problem: (B) Bear and Strings solution: http://ideone.com/0uBAjE
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int main() { int cnt, prev; char s[5005]; cnt=0; prev=-1; scanf("%s", s); if(strlen(s)>3) for(int i=0; i<strlen(s)-3; i++) if(s[i]=='b' && s[i+1]=='e' && s[i+2]=='a' && s[i+3]=='r') { cnt+=(strlen(s)-i-3)*(i-prev); prev=i; } printf("%d", cnt); return 0; }
note: you may have already realized that, subtracting "bear" substrings' from total length of string and multiplying it with "i" index subtracted by previous "i" index will give the result.
No comments:
Post a Comment