题目链接:Codeforces 385B Bear and Strings
记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAX_N = 5000 + 100; char str[MAX_N]; struct Node { int l, r; }; Node node[MAX_N / 3]; int main() { scanf("%s", str); int len = strlen(str); int cnt = 0; for(int i = 0; i < len - 3; i++) { if(str[i] == 'b' && str[i + 1] == 'e' && str[i + 2] == 'a' && str[i + 3] == 'r') { node[cnt].l = i; node[cnt].r = i + 3; cnt++; } } int res = 0; int r ,l , last = 0; for(int i = 0; i < cnt; i++) { l = node[i].l - last; r = len - node[i].r - 1; res += l; res += r; res += (l * r); res++; last = node[i].l + 1; } printf("%d\n", res); return 0;
Codeforces 385B Bear and Strings
原文:http://www.cnblogs.com/mengfanrong/p/4007064.html