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

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 xiand ci (1 ≤ xi ≤ nci — 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.

Sample test(s)
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.")

发现codefoce的题像脑筋急转弯,哈哈,松哥这么说的。

找规律,发现两种操作是互逆的,所以可以在写函数的时候采用以下方式

 1 #include <cstdio>
 2 using namespace std;
 3 
 4 char s[300004];
 5 
 6 int init(int n){
 7     for(int i = 0; i < n; i++) if(s[i] != '.') s[i] = '*';
 8     int ret = 0, t = 0;
 9     for(int i = 0; i < n; i++) {
10         if(s[i] == '*') {
11             if(t > 0) ret += t-1;
12             t = 0;
13         }
14         else t++;
15     }
16     if(t > 0) ret += t-1;//要考虑边界
17     return ret;
18 }
19 //对于两种操作是反转的情况
20 int solve(int n, int pos, int type){ //1: .->* 0: *->.
21     int ret = 0;
22     if(pos > 0) ret += (s[pos-1] == '.');
23     if(pos < n-1) ret += (s[pos+1] == '.');
24 
25     if(type == 1) ret *= -1;
26 
27     if(s[pos]=='.') s[pos] = '*';
28     else s[pos] = '.';
29     return ret;
30 }
31 int main()
32 {
33     int n, m;
34     while(~scanf("%d %d" , &n, &m)){
35         getchar();
36         gets(s);//读入一行的时候gets()是很好用的,但是Linux中不能用,要用getline();
37         int cur = init(n);
38         int pos;
39         char ch;
40         for(int i = 0; i < m; i++)
41         {
42             scanf("%d %c", &pos, &ch);
43             pos--;
44             if(ch != '.' && s[pos] != '.') ;
45             else if(ch == '.' && s[pos] == '.') ;
46             else cur += solve(n, pos, ch!='.');
47             printf("%d
", cur);
48         }
49     }
50 }

注意:读入一行的时候gets()是很好用的,但是Linux中不能用,要用getline();

当然这个题,开始傻傻的我用线段树了。。。代码自己看了都傻,就不贴了,还是借用模板打的,哎,好好学呀!

原文地址:https://www.cnblogs.com/shanyr/p/5209108.html