题意:一个字符串包含小写字母和’.’,化简是把两个连续的’.’化为一个’.’,现在操作是 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;
}


阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6