↓ Skip to main content
  1. Posts/

cf 570 C. Replacement (暴力)

·1010 words·3 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

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 x__i (1 ≤ x__i ≤ n) of string s is assigned value c__i. 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 x__i and_c__i_ (1 ≤ x__i ≤ n, c__i – a lowercas English letter or a period sign), describing the query of assigning symbol c__i to position x__i.

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

110 3
2.b..bz....
31 h
43 c
59 f

output

14
23
31

input

14 4
2.cc.
32 .
43 .
52 a
61 a

output

11
23
31
41

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.”)

对于每一个改变,如果是字母变成周期符号或者周期符号变成字母,我们称之为有效改变。

对于每个有效改变,只有当这个位置左边有周期符号的时候,会影响答案。

直接扫一遍即可,复杂度 O(n+m)。

抱歉之前代码贴错了……我的锅……

代码实现
 1/* ***********************************************
 2	> File Name: code/cf/#316/C.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com
 5	> Created Time: 2015年08月14日 星期五 00时54分50秒
 6************************************************ */
 7
 8#include <iostream>
 9#include <iomanip>
10#include <cstdio>
11#include <algorithm>
12#include <cmath>
13#include <cstring>
14#include <string>
15#include <map>
16#include <set>
17#include <queue>
18#include <vector>
19#include <stack>
20#define y0 abc111qqz
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define tm crazy111qqz
25#define lr dying111qqz
26using namespace std;
27#define REP(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef unsigned long long ULL;
30const int inf = 0x7fffffff;
31const int N=3E5+7;
32int n,m;
33char st[N];
34
35int judge(char a,char b)
36{
37   if (a>='a'&&a<='z'&&b>='a'&&b<='z') return 0;
38   if (a=='.'&&b=='.') return 0;
39   if (a>='a'&&a<='z'&&b=='.') return 1;
40   if (a=='.'&&b>='a'&&b<='z') return -1;
41}
42int main()
43{
44   cin>>n>>m;
45   getchar();
46   st[0]=' ';
47   for ( int i = 1 ; i <= n ; i++)
48   {
49	scanf("%c",&st[i]);
50   }
51   int cnt = 0;
52   for ( int i = 1 ;i <= n - 1 ; i++)
53   {
54	if (st[i]=='.'&&st[i+1]=='.')
55	{
56	    cnt++;
57	}
58   }
59  // cout<<"cnt :"<<cnt<<endl;
60   int x;
61   char ch;
62   for ( int i = 1 ; i <= m ; i++)
63   {
64	scanf("%d",&x);
65	getchar();
66	scanf("%c",&ch);
67	int flag =judge(st[x],ch);
68	st[x] = ch;
69	if (flag==0)
70	{
71	    cout<<cnt<<endl;
72	    continue;
73	}
74	if (flag==1)
75	{
76	    if (st[x-1]=='.') cnt++;
77	    if (st[x+1]=='.') cnt++;
78	    cout<<cnt<<endl;
79	    continue;
80	}
81	if (flag==-1)
82	{
83	    if (st[x-1]=='.') cnt--;
84	    if (st[x+1]=='.') cnt--;
85	    cout<<cnt<<endl;
86	    continue;
87	}
88   }
89   return 0;
90}

Related

cf 443B Kolya and Tandem Repeat

·445 words·1 min
B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.