zoukankan      html  css  js  c++  java
  • Codeforces 570C. Replacement

    Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as the following sequence of steps: find a substring ".." (two consecutive periods) in string s, of all occurrences of the substring let's choose the first one, and replace this substring with string ".". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains no two consecutive periods, then nothing happens.

    Let's define f(s) as the minimum number of operations of replacement to perform, so that the string does not have any two consecutive periods left.

    You need to process m queries, the i-th results in that the character at position xi (1 ≤ xi ≤ n) of string s is assigned value ci. After each operation you have to calculate and output the value of f(s).

    Help Daniel to process all queries.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 300 000) the length of the string and the number of queries.

    The second line contains string s, consisting of n lowercase English letters and period signs.

    The following m lines contain the descriptions of queries. The i-th line contains integer xi and ci(1 ≤ xi ≤ nci — a lowercas English letter or a period sign), describing the query of assigning symbol ci to position xi.

    Output

    Print m numbers, one per line, the i-th of these numbers must be equal to the value of f(s) after performing the i-th assignment.

    f(s)就是所有连续的'.'序列合起来的长度-序列的数量

    每次处理合并时观察两边的序列是否能合起来

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include <string>
    #include <sstream>
    #include <map>
    #include <cmath>
    #include <algorithm>
    #include <iomanip>
    #include <stack>
    #include <queue>
    #include <set>
    using namespace std;
    typedef long long LL;
    #define MOD 1000000007
    string s;
    int n,m;
    int ok[300005];
    int main()
    {
        freopen("test.in","r",stdin);
        cin >> n >> m;
        cin >> s;
        LL group(0),num(0);
        for (int i=0;i<n;i++){
            if (s[i] == '.'){
                num ++;
                if (i == 0 || s[i-1] != '.'){
                    group ++;
                }
                ok[i+1] = 1; // ok[i] 表示该位置是否为i
            }
        }
        for (int i=1;i<=n;i++){
            cout << ok[i];
        }cout << endl;
        for (int i=1;i<=m;i++){
            string ts;
            int d;
            cin >> d >> ts;
            char c = ts[0];
            int a = ok[d], b = (c == '.');
            if (a != b){
                if (a){
                    num --;
                }
                else {
                    num ++;
                }
                if (ok[d-1] && ok[d+1] && !b){
                    group ++;
                }
                if (ok[d-1] && ok[d+1] && b){
                    group --;
                }
                if (!ok[d-1] && !ok[d+1] && !b){
                    group --;
                }
                if (!ok[d-1] && !ok[d+1] && b){
                    group ++;
                }
            }
            ok[d] = b;
            cout << num - group << endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    hdu 4135 Co-prime (容斥定理)
    hdu 1509 Windows Message Queue (优先队列)
    poj 2104 K-th Number (划分树)
    hdu 1556 Color the ball (树状数组)
    海量大数据大屏分析展示一步到位:DataWorks数据服务对接DataV最佳实践
    使用MaxCompute Java SDK运行安全相关命令
    使用MaxCompute Java SDK 执行任务卡住了,怎么办?
    老代码多=过度耦合=if else?阿里巴巴工程师这样捋直老代码
    日志服务Python消费组实战(三):实时跨域监测多日志库数据
    如何限制用户仅通过HTTPS方式访问OSS?
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7007613.html
Copyright © 2011-2022 走看看