CodeForces
来源:互联网 发布:软件成本估算专家 编辑:程序博客网 时间:2024/05/17 12:19
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.
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.
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 h3 c9 f
431
4 4.cc.2 .3 .2 a1 a
1311
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.")
给出一个字符串,如果在字符串中遇到两个相邻的'.',把这两个点合并为一个点,直到找不到连续的两个点;
题目中有m条查询,每次查询都改变其中一个字符,如果直接暴力会被T的,所以要动动脑子了;
仔细观察会发现在一段连续的n个点中,需要进行n-1个操作使得n个点合并为一个点,那么假设现在有m段连续的点,其中每段的点的个数为a1,a2,a3...am,共计n个点;则需要进行的操作次数为(a1-1)+(a2-1)+(a3-1)+......+(am-1)=a1+a2+a3+......+am-m=n-m;
可知,最终结果应为点的总个数减去区间数;
代码奉上:
#include <iostream>using namespace std;int main(){int n, m;cin >> n >> m;string s;cin >> s;int cnt, block, len=s.size();//cnt表示'.'的总数,block表示区间数;cnt=block=0;bool flag=false;for(int i=0; i<len; i++){if(s[i]=='.') cnt++;if(s[i]=='.'&&!flag){block++;flag=true;}if(s[i]!='.') flag=false;}int x;string op;for(int i=0; i<m; i++){cin >> x >> op;x--; //题目中字符串由1开始计数;//只有给出的字符与原字符不同时结果才会改变; if(s[x]=='.'){if(op[0]!='.'){s[x]=op[0];//改变原字符串;cnt--;if(x==0&&s[x+1]!='.') block--;else if(x==len-1&&s[x-1]!='.') block--;else if(x<len-1 && x>0){if(s[x-1]=='.'&&s[x+1]=='.') block++;else if(s[x-1]!='.'&&s[x+1]!='.') block--; }}}else{if(op[0]=='.'){s[x]=op[0];cnt++;if(x==0&&s[x+1]!='.') block++;else if(x==len-1&&s[x-1]!='.') block++;else if(x>0&&x<len-1){if(s[x+1]=='.'&&s[x-1]=='.') block--;else if(s[x+1]!='.'&&s[x-1]!='.') block++;}}}//cout << "cnt: " << cnt << " block: " << block << endl;cout << cnt-block << endl;}return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- HDU5500 Reorder the Books(脑洞?)
- Rxjava
- 手机平板charger模块前期bringup主要工作
- oracle的权限管理
- java垃圾回收机制 gc
- CodeForces
- POJ 2352 Stars & UESTC 1584 Washi与Sonochi的约定 排序+树状数组
- 简单进度条
- oracle的基本操作
- __autoload()和spl_autoload_register()的理解
- 经济民族主义的前夜?普华永道思略特全球创新1000强解读
- 阿尔法元并未否定AI需要“人类师父”
- 中科院等发布《2017研究前沿》 中国25个前沿表现卓越 居全球第二
- 开发的数字货币钱包客户端的5个常见问题