codeforces 570 c

C. Replacement

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Daniel has a string s, consisting of lowercase English letters and period signs (characters ‘.‘).
Let‘s define the operation of replacementas 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.

Sample test(s)

input

10 3
.b..bz....
1 h
3 c
9 f

output

4
3
1

input

4 4
.cc.
2 .
3 .
2 a
1 a

output

1
3
1
1

Note

Note to the first sample test (replaced periods are enclosed in square brackets).

The original string is ".b..bz....".

  • after the first query f(hb..bz....) =
    4    ("hb[..]bz...." ?→? "hb.bz[..].." ?→? "hb.bz[..]." ?→? "hb.bz[..]" ?→?"hb.bz.")
  • after the second query f(hbс.bz....) =
    3    ("hbс.bz[..].." ?→? "hbс.bz[..]." ?→? "hbс.bz[..]" ?→? "hbс.bz.")
  • after the third query f(hbс.bz..f.) =
    1    ("hbс.bz[..]f." ?→? "hbс.bz.f.")

Note to the second sample test.

The original string is ".cc.".

  • after the first query: f(..c.) =
    1    ("[..]c." ?→? ".c.")
  • after the second query: f(....) =
    3    ("[..].." ?→? "[..]." ?→? "[..]" ?→? ".")
  • after the third query: f(.a..) =
    1    (".a[..]" ?→? ".a.")
  • after the fourth query: f(aa..) =
    1    ("aa[..]" ?→? "aa.")
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

#define N 300000 + 10

int n, m;
char s[N];

int doit()
{
    int res = 0;
    int cnt = 0;
    for(int i = 1; i <= n; i++)
    {
        if(s[i] == '.') cnt++;
        else
        {
            if(cnt > 1) res += cnt - 1;
            cnt = 0;
        }
    }
    if(cnt > 1)
    res += cnt - 1;
    return res;
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        scanf("%s", s + 1);
        char c;
        int t;
        int ans = doit();
        for(int i = 0; i < m; i++)
        {
            scanf("%d %c", &t, &c);
            if(s[t] != '.' && c == '.')
            {
                s[t] = c;
                if(t - 1 > 0 && s[t - 1] == '.') ans += 1;
                if(t + 1 <= n && s[t + 1] == '.') ans += 1;
            }
            else if(s[t] == '.' && c != '.')
            {
                s[t] = c;
                if(t - 1 > 0 && s[t - 1] == '.') ans -= 1;
                if(t + 1 <= n && s[t + 1] == '.') ans -= 1;
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}

/*

10 3
.b..bz....
1 h
3 c
9 f

4 4
.cc.
2 .
3 .
2 a
1 a

2 7
ab
1 w
2 w
1 c
2 .
2 .
1 .
2 b

*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 02:39:46

codeforces 570 c的相关文章

codeforces 570 D. Tree Requests 树状数组+dfs搜索序

链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Roman planted a tree consisting of n vertices. Each vertex contains a low

codeforces 570 D. Tree Requests (dfs)

题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变成一个回文串? 解题思路: 判断是不是回文串,可以统计集合中出现过的字母的个数,出现奇数次的字母个数小于1,即为回文串,否则不是.所以我们可以使用状压统计当前区间中字母出现的奇偶次数. 对于如何快速的求出区间,先dfs整棵树,标记下来每个节点进栈的时间和出栈的时间,然后把高度一样的点按照进栈时间顺序

codeforces 570 E. Pig and Palindromes (DP)

题目链接: 570 E. Pig and Palindromes 题目描述: 有一个n*m的矩阵,每个小格子里面都有一个字母.Peppa the Pig想要从(1,1)到(n, m).因为Peppa the Pig是一个完美主义者,她想要她所经过的路径上的字母组成的字符串是一个回文串,现在Peppa the Pig想要知道有多少满足条件的走法? 解题思路: 因为经过路径上的字母要组成回文串,所以可以从(1,1),(n,m)同时开始dp.从(1,1)出发只能向下方和右方走,从(n,m)出发只能向上

codeforces 570 E. Pig and Palindromes

题意:给出n*m的字母表,求从左上角走到右下角能形成多少个回文串,只能往下或往右走. 做法:dp[r1][c1][r2][c2],从左上角走到(r1,c1),从右下角走到(r2,c2)时,能形成多少个回文串,因为爆内存,表示成dp[step][r1][r2],从左上角走到r1行,从右下角走到r2行,分别走了step步时,能形成多少个回文串,因为c1=step+2-r1,c2=n+m-step-r2,所以是一样的,这样差不多能过了,因为两边最多走250步,所以需要的空间是250*500*500,当

Codeforces 570 B.Simple Game

click here ~~ ***B. Simple Game*** One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a. Then, by using a random ge

codeforces 570 D Tree Requests

题意:给出一棵树.每一个结点都有一个字母,有非常多次询问,每次询问.以结点v为根的子树中高度为h的后代是否可以经过调整变成一个回文串. 做法: 推断能否够构成一个回文串的话,仅仅须要知道是否有大于一个的奇数数目的字母就可以.为了非常快的訪问到一个区间.记录前缀和就可以.为了省内存,状压奇偶就可以. 为了非常快的找到以结点v为根的子树中高度为h的后代,须要dfs整棵树.然后记录每一个结点第一次訪问它的时间戳以及离开它的时间戳,就能够二分出来. 为了省内存,能够离线处理询问. #include<ma

Codeforces 570 A. Elections

click here ~~ ***A. Elections*** The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate. The electoral system in the country is pretty un

Codeforces Round #570 (Div. 3 )A

A. Nearest Interesting Number 题目链接:http://codeforces.com/contest/1183/problem/A 题目: Polycarp knows that if the sum of the digits of a number is divisible by 3, then the number itself is divisible by 3. He assumes that the numbers, the sum of the digi

Codeforces Round #570 (Div. 3) B. Equalize Prices

#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<set> #define ll long long using namespace std; const int maxn = 200010; const int inf = 0x3f3f3f3f; int main() { int t;