Codeforces Round #316 (Div. 2) C 思路/模拟
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 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.
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.
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.")
题意:给你长度为n的字符串 m组替换和查询xi and ci 查询 替换之后字符串中有多少个[..]
题解: 先统计一下初始的字符串有多少组[..]
然后考虑每次替换对[..]有什么影响呢 ?若ci为‘.’ 才有可能增加答案 若为其他字符才有可能减少答案
并且只能与 xi + 1,xi - 1两个位置产生影响 最多增加或减少两个[..]
细细分析每种情况。注意边界判断/注意替换前x位置是什么字符。 具体看代码。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>
#define ll __int64
#define mod 1e9+7
#define PI acos(-1.0)
using namespace std;
int n,m;
char a[];
int main()
{
scanf("%d %d",&n,&m);
getchar();
scanf("%c",&a[]);
int ans=;
for(int i=;i<=n;i++)
{
scanf("%c",&a[i]);
if(a[i]=='.'&&a[i-]=='.')
ans++;
}
int pos;
char exm;
for(int i=;i<=m;i++)
{
scanf("%d %c",&pos,&exm);
if(exm=='.')
{
if(pos->=&&a[pos]!='.')
{
if(a[pos-]=='.')
ans++;
}
if(pos+<=n&&a[pos]!='.')
{
if(a[pos+]=='.')
ans++;
}
a[pos]=exm;
}
else
{
if(pos->=&&a[pos]=='.')
{
if(a[pos-]=='.')
ans--;
}
if(pos+<=n&&a[pos]=='.')
{
if(a[pos+]=='.')
ans--;
}
a[pos]=exm;
}
cout<<ans<<endl;
}
return ;
}
Codeforces Round #316 (Div. 2) C 思路/模拟的更多相关文章
- Codeforces Round #371 (Div. 2) C 大模拟
http://codeforces.com/contest/714/problem/C 题目大意:有t个询问,每个询问有三种操作 ①加入一个数值为a[i]的数字 ②消除一个数值为a[i]的数字 ③给一 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #543 (Div. 2) D 双指针 + 模拟
https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...
- Codeforces Round #398 (Div. 2) A. Snacktower 模拟
A. Snacktower 题目连接: http://codeforces.com/contest/767/problem/A Description According to an old lege ...
- Codeforces Round #316 (Div. 2) (ABC题)
A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利. 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序) ...
- Codeforces Round #316 (Div. 2) C. Replacement
题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...
- 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了. 总结:下次一定要注意了,提交前一定要看 ...
随机推荐
- [转] 防止js全局变量污染方法总结
javaScript 可以随意定义保存所有应用资源的全局变量.但全局变量可以削弱程序灵活性,增大了模块之间的耦合性.在多人协作时,如果定义过多的全局变量 有可能造成全局变量冲突,也就是全局变量污染问题 ...
- 转:Python集合(set)类型的操作
转自:http://blog.csdn.net/business122/article/details/7541486 python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系 ...
- SVG path
在网页上画一图形,比如星星或波浪线,开始是想着图形软件画一个的,后来发现SVG这绘图程序的语言,感觉甚是可以,就发了些时间学了一下,在此做一简单分享和记录. 菜鸟上是这么介绍的(SVG 是使用 XML ...
- Ansible指令和常用模块使用
这里文章记录一下ansible的指令选项和常用的模块使用 ansible指令选项 -m:要执行的模块,默认为command -a:模块的参数 -u:ssh连接的用户名,默认用root,ansible. ...
- k8s的secret基本概念及案例
secret相对于configMap,功能上是相似的但是secret是以其他编码方式去记录配置信息的,但是也可以被解读,只不过有技术门槛,不是那么容易就被解读.使用base64可以解码:echo ** ...
- k8s的pv和pvc简述
pvc:资源需要指定:1.accessMode:访问模型:对象列表: ReadWriteOnce – the volume can be mounted as read-write by a s ...
- Freemaker基于word模板动态导出汇总整理
Freemaker基于word模板动态导出汇总整理 一.使用的jar包: 二.Word模板动态导出的基本思路: 1.首先通过自己在word中创建好需要导出的word文本+表格的模板,模板中需要填写内容 ...
- videojs的使用
[官网]http://www.videojs.com/ videojs就提供了这样一套解决方案,他是一个兼容HTML5的视频播放工具,早期版本兼容所有浏览器,方法是:提供三个后缀名的视频,并在不支持h ...
- 科学计算库Numpy——数值计算
矩阵 求和 乘积 最大值和最小值 最大值和最小值的位置 平均数 标准差 方差 限制 四舍五入
- 模型类序列化器ModelSerializer
定义 比如我们创建一个BookInfoSerializer class BookInfoSerializer(serializers.ModelSerializer): ""&qu ...