CodeForces570C - Replacement

来源:互联网 发布:linux 高性能c日志库 编辑:程序博客网 时间:2024/04/24 11:55

Description

Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation ofreplacement as the following sequence of steps: find a substring ".." (two consecutive periods) in strings, 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 ofreplacement 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 strings is assigned value ci. After each operation you have to calculate and output the value off(s).

Help Daniel to process all queries.

Input

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

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

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

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 Input

Input
10 3.b..bz....1 h3 c9 f
Output
431
Input
4 4.cc.2 .3 .2 a1 a
Output
1311

Hint

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.")


丹尼尔有一个字符串S,包括小写英文字母和符号('.')。让我们来定义替代的操作顺序如下步骤:找到一个字符串“..”(连续两个)所有中我们选第一个。换言之,在更换操作期间,前两个连续的'..'换成一个'.' , 如果字符串S不包含两个连续的‘..’,则什么也没有发生。
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.
让我们将F(S)定义为替换执行的最小操作数,以便该字符串没有任何两个连续的周期。
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.
你需要处理m个查询,
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.

题意:给你一个由小写字母和字符'.'组成的字符串,定义了一种操作:若字符串中含有连续两个"." 则将其替换为一个".",持续这样的操作,直到整个字符串再也不能进行这样的操作,定义一个映射f :(string)s->(int)n 给出一个字符串被改写成再也执行不了上述操作所需的操作次数。
给出若干条询问,每次询问都将字符串中的第i位替换为指定的字符,注意:每次询问时修改的字符串是对上一次修改后没有进行操作的字符串进行替换(如果是第一条询问则是对原始字符串进行替换)
求每次询问对应的字符串的f值 即f(s)

#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<algorithm>using namespace std;#define maxn 300010int n,m,pos,last = 0;char s[maxn],rp[2];bool flag[maxn];int main(void){scanf("%d%d\n",&n,&m);gets(s+1);flag[0]=false,flag[n+1]=false;for(int i=1;i<=n;i++){if(s[i]=='.'){flag[i] = true;if(flag[i-1])last++;}else flag[i] = false;}//cout<<last;bool mark = 0;for(int i=1;i<=m;i++){scanf("%d ",&pos);scanf("%s",rp);*rp=='.'?mark=1:mark=0;if(mark){if(!flag[pos]){flag[pos] = true;if(flag[pos-1]&&flag[pos+1])last+=2;else if(flag[pos-1]||flag[pos+1])last++;}}else{if(flag[pos]){flag[pos] = false;if(flag[pos-1]&&flag[pos+1])last-=2;else if(flag[pos-1]||flag[pos+1])last-=1;}}printf("%d\n",last);}return 0;}


0 0
原创粉丝点击