CodeForces - 570C Replacement

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
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.")
思维题, 题意如下;

给出一个字符串,如果在字符串中遇到两个相邻的'.',把这两个点合并为一个点,直到找不到连续的两个点;

题目中有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;
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值