Replacement(思维题)
2 seconds
256 megabytes
standard input
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.
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 ≤ n, ci — a lowercas English letter or a period sign), describing the query of assigning symbol ci to position xi.
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.
10 3
.b..bz....
1 h
3 c
9 f
4
3
1
4 4
.cc.
2 .
3 .
2 a
1 a
1
3
1
1
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(思维题)的更多相关文章
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- 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 ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- 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 ...
- 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 ...
- cf796c 树形,思维题
一开始以为是个树形dp,特地去学了..结果是个思维题 /* 树结构,设最大点权值为Max,则答案必在在区间[Max,Max+2] 证明ans <= Max+2 任取一个点作为根节点,那么去掉这个 ...
- BZOJ4401: 块的计数 思维题
Description 小Y最近从同学那里听说了一个十分牛B的高级数据结构——块状树.听说这种数据结构能在sqrt(N)的时间内维护树上的各种信息,十分的高效.当然,无聊的小Y对这种事情毫无兴趣,只是 ...
随机推荐
- Java.util.Map排序输出
在java的众多Map实现中,Map基本上是不能保证顺序的(LinkedHashMap可以保证插入顺序或者访问顺序,TreeMap默认按照key升序但可以自定义Comparator),在开发过程中当数 ...
- CET-4- translation1
questions 2017/10/17 多年来,家长和老手都曾得到过这样一种信息(message):尽量利用任何机会表扬孩子,对他们所干的任何事情都要说好.据说这样做有助于提高孩子的自尊.但是近来许 ...
- Java JTS & 空间数据模型
空间数据模型 判断两个几何图形是否存在指定的空间关系.包括: 相等(equals).分离(disjoint).相交(intersect).相接(touches).交叉(crosses).包含于(wit ...
- DeepLearning.ai学习笔记(三)结构化机器学习项目--week2机器学习策略(2)
一.进行误差分析 很多时候我们发现训练出来的模型有误差后,就会一股脑的想着法子去减少误差.想法固然好,但是有点headlong~ 这节视频中吴大大介绍了一个比较科学的方法,具体的看下面的例子 还是以猫 ...
- myecplise自带的tomcat问题
今天做一个项目时候,发现myecplise自带的tomcat上面部署了是可以运行的,可是当部署到自己下载的tomcat时候,就报错,tomcat可以启动,项目无法启动,查了问题,发现是web,xml中 ...
- Xamarin Android 中Acitvity如何传递数据
在xamarin android的开发中,activity传递数据非常常见,下面我也来记一下在android中activity之间传递数据的几种方式, Xamarin Android中Activity ...
- 虚拟机创建流程中neutron代码分析(三)
前言: 当neutron-server创建了port信息,将port信息写入数据库中.流程返回到nova服务端,接着nova创建的流程继续走.在计算节点中neutron-agent同样要完成很多的工作 ...
- Java容器---List
List 承诺可以将元素维护在特定的序列中.List 接口在Collection的基础上添加了大量的方法,使得可以在List的中间插入和移除元素. 有两种类型的List: -----基本的 ...
- python爬虫爬取人人车(二手车)、利用padas、matplotlib生成图表,将信息打成csv格式
该程序主要为了抓取人人车卖车信息,包括车系.车型号.购车日期.卖车价格.行驶路程.首付价格等等信息.话不多说直接代码. 入库之后将Mongodb里的信息导出成Excel语句 mongoexport - ...
- Head First设计模式之策略模式
这是学习的第一个设计模式,而书中写的实例相对比较复杂,参考了网上的文章进行总结 一.定义 策略模式(strategy pattern): 定义了算法族, 分别封闭起来, 让它们之间可以互相替换, 此模 ...