题意:一个字符串包含小写字母和’.’,化简是把两个连续的’.’化为一个’.’,现在操作是 a b,把位置a上的字符替换成b后,问多少步才能化简结束。
题解:先把初始字符串需要的步数计算出来,然后分情况加或减或不变。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 300005;
int n, m;
char str[N], s[N], num[N];
int main() {
scanf("%d%d", &n, &m);
scanf("%s", str + 1);
int cnt = 0;
for (int i = 1; i < n; i++)
if (str[i] == '.' && str[i + 1] == '.')
cnt++;
char c;
int pos;
while (m--) {
scanf("%d %c", &pos, &c);
if (str[pos] == '.' && c == '.') {
printf("%d\n", cnt);
continue;
}
if (str[pos] != '.' && c != '.') {
printf("%d\n", cnt);
continue;
}
if (pos == n) {
if (c != '.' && str[pos - 1] == '.')
cnt--;
if (c == '.' && str[pos - 1] == '.')
cnt++;
}
else if (pos == 1) {
if (c != '.' && str[pos + 1] == '.')
cnt--;
if (c == '.' && str[pos + 1] == '.')
cnt++;
}
else {
if (c == '.' && str[pos - 1] == '.' && str[pos + 1] == '.')
cnt += 2;
else if (c != '.' && str[pos - 1] == '.' && str[pos + 1] == '.')
cnt -= 2;
else if (c != '.' && (str[pos - 1] == '.' || str[pos + 1] == '.'))
cnt--;
else if (c == '.' && (str[pos - 1] == '.' || str[pos + 1] == '.'))
cnt++;
}
printf("%d\n", cnt);
str[pos] = c;
}
return 0;
}