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

发现codefoce的题像脑筋急转弯,哈哈,松哥这么说的。

找规律,发现两种操作是互逆的,所以可以在写函数的时候采用以下方式

 #include <cstdio>
using namespace std; char s[]; int init(int n){
for(int i = ; i < n; i++) if(s[i] != '.') s[i] = '*';
int ret = , t = ;
for(int i = ; i < n; i++) {
if(s[i] == '*') {
if(t > ) ret += t-;
t = ;
}
else t++;
}
if(t > ) ret += t-;//要考虑边界
return ret;
}
//对于两种操作是反转的情况
int solve(int n, int pos, int type){ //1: .->* 0: *->.
int ret = ;
if(pos > ) ret += (s[pos-] == '.');
if(pos < n-) ret += (s[pos+] == '.'); if(type == ) ret *= -; if(s[pos]=='.') s[pos] = '*';
else s[pos] = '.';
return ret;
}
int main()
{
int n, m;
while(~scanf("%d %d" , &n, &m)){
getchar();
gets(s);//读入一行的时候gets()是很好用的,但是Linux中不能用,要用getline();
int cur = init(n);
int pos;
char ch;
for(int i = ; i < m; i++)
{
scanf("%d %c", &pos, &ch);
pos--;
if(ch != '.' && s[pos] != '.') ;
else if(ch == '.' && s[pos] == '.') ;
else cur += solve(n, pos, ch!='.');
printf("%d\n", cur);
}
}
}

注意:读入一行的时候gets()是很好用的,但是Linux中不能用,要用getline();

当然这个题,开始傻傻的我用线段树了。。。代码自己看了都傻,就不贴了,还是借用模板打的,哎,好好学呀!

Replacement(思维题)的更多相关文章

  1. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  2. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  3. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  4. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  5. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  7. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  8. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  9. cf796c 树形,思维题

    一开始以为是个树形dp,特地去学了..结果是个思维题 /* 树结构,设最大点权值为Max,则答案必在在区间[Max,Max+2] 证明ans <= Max+2 任取一个点作为根节点,那么去掉这个 ...

  10. BZOJ4401: 块的计数 思维题

    Description 小Y最近从同学那里听说了一个十分牛B的高级数据结构——块状树.听说这种数据结构能在sqrt(N)的时间内维护树上的各种信息,十分的高效.当然,无聊的小Y对这种事情毫无兴趣,只是 ...

随机推荐

  1. C#创建对象时各种初始化属性、字段的方式的执行顺序

    创建对象代码如下: new FilterInfo(Student.CreateTimeProperty,"朱七",Express.Equals,Relationship.Or) { ...

  2. Arcade初探[0] 目录与导航

    2017年6月,ESRI开发者页面出现了一个新玩意儿:Arcade. 连接:点我 这是什么东西呢?有什么用呢? 1. 是什么 Arcade一种表达语言,可以在ArcGIS平台上使用.不管是编写简单的脚 ...

  3. bzoj 3528: [Zjoi2014]星系调查

    Description 银河历59451年,在银河系有许许多多已被人类殖民的星系.如果想要在行 星系间往来,大家一般使用连接两个行星系的跳跃星门.  一个跳跃星门可以把 物质在它所连接的两个行星系中互 ...

  4. Swift学习第一天--面向过程

    //: Playground - noun: a place where people can play import UIKit //---------------------- Hello wor ...

  5. LAMP第四部分mysql操作

    1. 忘记root密码编辑mysql主配置文件 my.cnf 在[mysqld]字段下添加参数  skip-grant  ,重启数据库服务,这样就可以进入数据库不用授权了 mysql -uroot , ...

  6. LAMP第二部分apache配置

    课程大纲:1. 下载discuz! mkdir /data/wwwcd /data/wwwwget  http://download.comsenz.com/DiscuzX/3.2/Discuz_X3 ...

  7. 微信公众号开发——通过ffmpeg解决amr文件无法播放问题

    今天刚好碰到个需求,要在微信浏览器中实现录音,并在其他页面上播放.录音功能本身是JS SDK的功能,倒没啥问题,然而录音的文件保存下来是amr格式,而IOS的浏览器没法播放amr(据说微信浏览器的vi ...

  8. windows上安装redis

    The Redis project does not officially support Windows. However, the Microsoft Open Tech group develo ...

  9. 访问vm中centos的web站点

    vm网络连接设置成NAT 需要把centos设置成静态IP 再不行,记得把centos的防火墙先关闭

  10. javascript中name,value等属于保留字

    前几天在练习js代码的时候,碰到了一个坑,这是让人醉了. html代码如下: <div> <div> <!--输入 123456--> <lable>请 ...