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 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 ≤ n, ci — 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.

Examples
Input
  1. 10 3
    .b..bz....
    1 h
    3 c
    9 f
Output
  1. 4
    3
    1
Input
  1. 4 4
    .cc.
    2 .
    3 .
    2 a
    1 a
Output
  1. 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.")

题意:给你长度为n的字符串  m组替换和查询xi and ci   查询 替换之后字符串中有多少个[..]

题解: 先统计一下初始的字符串有多少组[..]

然后考虑每次替换对[..]有什么影响呢 ?若ci为‘.’ 才有可能增加答案 若为其他字符才有可能减少答案

并且只能与 xi + 1,xi - 1两个位置产生影响  最多增加或减少两个[..]

细细分析每种情况。注意边界判断/注意替换前x位置是什么字符。 具体看代码。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<queue>
  5. #include<stack>
  6. #include<vector>
  7. #include<map>
  8. #include<algorithm>
  9. #define ll __int64
  10. #define mod 1e9+7
  11. #define PI acos(-1.0)
  12. using namespace std;
  13. int n,m;
  14. char a[];
  15. int main()
  16. {
  17. scanf("%d %d",&n,&m);
  18. getchar();
  19. scanf("%c",&a[]);
  20. int ans=;
  21. for(int i=;i<=n;i++)
  22. {
  23. scanf("%c",&a[i]);
  24. if(a[i]=='.'&&a[i-]=='.')
  25. ans++;
  26. }
  27. int pos;
  28. char exm;
  29. for(int i=;i<=m;i++)
  30. {
  31. scanf("%d %c",&pos,&exm);
  32. if(exm=='.')
  33. {
  34. if(pos->=&&a[pos]!='.')
  35. {
  36. if(a[pos-]=='.')
  37. ans++;
  38. }
  39. if(pos+<=n&&a[pos]!='.')
  40. {
  41. if(a[pos+]=='.')
  42. ans++;
  43. }
  44. a[pos]=exm;
  45. }
  46. else
  47. {
  48. if(pos->=&&a[pos]=='.')
  49. {
  50. if(a[pos-]=='.')
  51. ans--;
  52. }
  53. if(pos+<=n&&a[pos]=='.')
  54. {
  55. if(a[pos+]=='.')
  56. ans--;
  57. }
  58. a[pos]=exm;
  59. }
  60. cout<<ans<<endl;
  61. }
  62. return ;
  63. }

Codeforces Round #316 (Div. 2) C 思路/模拟的更多相关文章

  1. Codeforces Round #371 (Div. 2) C 大模拟

    http://codeforces.com/contest/714/problem/C 题目大意:有t个询问,每个询问有三种操作 ①加入一个数值为a[i]的数字 ②消除一个数值为a[i]的数字 ③给一 ...

  2. Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)

    A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  3. Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】

    A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  4. Codeforces Round #436 (Div. 2)C. Bus 模拟

    C. Bus time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input out ...

  5. Codeforces Round #543 (Div. 2) D 双指针 + 模拟

    https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...

  6. Codeforces Round #398 (Div. 2) A. Snacktower 模拟

    A. Snacktower 题目连接: http://codeforces.com/contest/767/problem/A Description According to an old lege ...

  7. Codeforces Round #316 (Div. 2) (ABC题)

    A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利. 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序) ...

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

    题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...

  9. Codeforces Round #316 (Div. 2) B. Simple Game

    思路:把n分成[1,n/2],[n/2+1,n],假设m在左区间.a=m+1,假设m在右区间,a=m-1.可是我居然忘了处理1,1这个特殊数据.被人hack了. 总结:下次一定要注意了,提交前一定要看 ...

随机推荐

  1. 踩坑日志!viser-ng的使用

    在ng-alian项目中使用viser图表库,在app.module中引用了viser-ng,然而,在具体的html项目中使用<v-chart>会报错,提示v-chart不是一个angul ...

  2. GPU并行编程:内核及函数的实现

    原文链接 回想一下我们之前在设备上使用“kernelFunction<<<1,1>>>(..)”执行一个函数的代码,我在那里还曾说过后面会细说,本文就详细介绍一下参 ...

  3. 安装软件出现缺少vcruntime140dll的解决方法

    转自:http://jingyan.baidu.com/article/49711c617e4000fa441b7c92.html 首先下载vc++2015,注意自己系统是32位还是64位的,下载对应 ...

  4. css中如何把鼠标变成手

    css中鼠标放上去变成手型怎么设置:其实就是一个属性的问题, css的cursor属性 cursor:pointer; 其实这个属性我也记了很多,到现在都容易拼写错误,不过好在编辑器有提示. defa ...

  5. testC-I

    总时间限制:  20000ms 单个测试点时间限制:  1000ms 内存限制:  128000kB 描述 给你一组数,a1,a2,a3,⋯,an. 令:G=gcd(a1,a2,a3,⋯,an) 现在 ...

  6. Session 会话保持

    本文将详细讨论session的工作机制并且对在Java web application中应用session机制时常见的问题作出解答 一.术语session session,中文经常翻译为会话,其本来的 ...

  7. ubuntu下vim的简单配置

    该文章只是进行符合自己习惯的最基本的配置,更加高级的配置请参考更加有含量的博文! 1.打开vim下的配置文件 sudo vim /etc/vim/vimrc 2.在这个文件中,会有这么一句:synta ...

  8. Oracle redo与undo 第二弹

    首先看一下undo与redo的字面意思:   undo:撤销,也就是取消之前的操作.   redo:重做,重新执行一遍之前的操作. 什么是REDO REDO记录transaction logs,分为o ...

  9. 6、python中的元组

    元组(tuple)是python中有序.不可变的数据结构.元组还是python四种数据结构中唯一一种不可变的数据结构. 一.前言 元组在很多方面都变现得跟列表一样,除了列表储存得对象是可变得,而元组储 ...

  10. 小知识(h5 js )

    1.如果都为NaN但是他们不相等var a=NaN;var b=NaN;a==b //flase2.javascript 是一种脚本语言,可以创建服务器端和客户端的脚本3.javascript 中有两 ...