hdu 4046 BF+树状数组
题目不难,比赛的时候一读完题目我就知道怎样做了,可是打了一个小时,原因是BF算法居然记不清了,唉,早知道干脆用KMP了。可是打完以后还一直不过,让其它几个队帮我测试,测了一个多小时还是没有找到错误。离比赛结束只有一个小时的时候,让海峰帮我测,终于找到错误,改正就过了。唉,这次比赛的悲剧也就在我,在这题上了吧,花了太多时间。
/*
* 2011BeijingG/win.cpp
* Created on: 2011-9-18
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAXN = 50100;
const char pattern[10] = { " wbw" };
char str[MAXN];
int N;
int array[MAXN];
bool match[MAXN];
inline bool myequals(const char *str1, const char *str2) {
if (str1[0] != str2[0]) {
return false;
}
if (str1[1] != str2[1]) {
return false;
}
if (str1[2] != str2[2]) {
return false;
}
return true;
}
inline int lowbit(int x) {
return x & (-x);
}
int sum(int n) {
int ret = 0;
for (int i = n; i > 0; i -= lowbit(i)) {
ret += array[i];
}
return ret;
}
void update(int index, int value) {
for (int i = index; i <= N; i += lowbit(i)) {
array[i] += value;
}
}
void mybf() {
int i;
int len = N;
for (i = 1; i < len - 1; i++) {
if (str[i] == 'w' && str[i + 1] == 'b' && str[i + 2] == 'w') {
update(i + 2, 1);
match[i + 2] = true;
}
}
}
void work();
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
work();
return 0;
}
void work() {
int T, M, op, a, b;
char c;
bool flag;
scanf("%d", &T);
for (int t = 1; t <= T; t++) {
printf("Case %d:\n", t);
memset(match, 0, sizeof(match));
memset(array, 0, sizeof(array));
scanf("%d%d", &N, &M);
if (N > 0) {
scanf("%s", &str[1]);
}
mybf();
while (M--) {
scanf("%d", &op);
if (op == 0) {
scanf("%d%d", &a, &b);
if (b - a < 2) {
puts("0");
} else {
printf("%d\n", sum(b + 1) - sum(a + 2));
}
} else {
scanf("%d %c", &a, &c);
if (str[++a] == c) {
continue;
}
str[a] = c;
for (int ii = 0; ii < 3; ii++) {
if (a > N) {
break;
}
if (a > 2) {
flag = myequals(&str[a - 2], &pattern[1]);
if (flag && !match[a]) {
update(a, 1);
} else if (!flag && match[a]) {
update(a, -1);
}
match[a] = flag;
}
a++;
}
}
}
}
}