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;
}

时间: 2024-12-17 02:39:10

Codeforces 570C. Replacement的相关文章

[短期持续更新]Codeforces 构造题一览

说实话我觉得做这种题很没意思(不够硬核), 可是人有短板终究是要补的...起码这种类型补起来相对简单 所以还是把先前准备好的专题放下吧,做点实现上比较休闲的题 ps.为了精简篇幅,代码全部丢到ubuntu上 Codeforces - 483C 题意:给定\(k\),构造\(n\)的某一排列\(p\)满足\(|p_i-p_{i+1}|\)共有\(k\)种不同的值 做法:当\(k\)为\(n-1\)时,分奇偶前后插可满足情况,\(k<n-1\)时从\(k\)处开始排序可消除差异 https://pa

Codeforces Round #316 (Div. 2)C. Replacement(模拟)

传送门 Description 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 st

Codeforces 825D Suitable Replacement - 贪心 - 二分答案

You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbi

Codeforces Round #316 (Div. 2) C. Replacement

题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符,问每次须要多少次合并次数.才使字符串没有' .. ' 思路:最原始的想法,就是对于每一次询问,都遍历整个字符串.这样时间复杂度o(n*m),就高达10^10方,非常明显会tle. 换下思路,事实上每次询问所改变的字符都会保留到下一次.也就是下一次的次数就会受到上一次的影响,那么我仅仅要就算出第一次的

Educational Codeforces Round 25 D - Suitable Replacement(贪心)

题目大意:给你字符串s,和t,字符串s中的'?'可以用字符串t中的字符代替,要求使得最后得到的字符串s(可以将s中的字符位置两两交换,任意位置任意次数)中含有的子串t最多. 解题思路: 因为知道s中的字符位置可以交换无数次,所以只要s中含有可以组成t的字符就一定可以出现子串t.所以就是要令t中的字符数量尽量平均地分布在s中. 代码: 1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int str[30

Codeforces Round #316 (Div. 2) C Replacement 扫描法

先扫描一遍得到每个位置向后连续的'.'的长度,包含自身,然后在扫一遍求出初始的合并次数. 对于询问,只要对应位置判断一下是不是'.',以及周围的情况. #include<bits/stdc++.h> using namespace std; const int maxn = 3e5+5; char s[maxn]; int post[maxn]; int main() { //freopen("in.txt","r",stdin); int n,m; s

codeforces Gym 100187L L. Ministry of Truth 水题

L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/K Description Andrey works in the Ministry of Truth. His work is changing articles in newspapers and magazines so that they praise the Party an

Codeforces Round #296 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/527 A. Playing with Paper time limit per test:2 seconds memory limit per test:256 megabytes One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm ?×? b mm sheet

CodeForces 527B Error Correct System

Error Correct System Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 527B Description Ford Prefect got a job as a web developer for a small company that makes towels. His current work ta