-
Notifications
You must be signed in to change notification settings - Fork 152
/
1609B-WilliamTheVigilant.cpp
35 lines (31 loc) · 1.39 KB
/
1609B-WilliamTheVigilant.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
int main(){
std::ios_base::sync_with_stdio(false);
long n, q; std::cin >> n >> q;
std::string s; std::cin >> s;
long cnt(0);
for(long p = 2; p < s.size(); p++){cnt += (s[p - 2] == 'a' && s[p - 1] == 'b' && s[p] == 'c');}
while(q--){
long w; char c; std::cin >> w >> c;
--w;
if(s[w] != c && c == 'a'){
if(w >= 2 && s[w - 2] == 'a' && s[w - 1] == 'b' && s[w] == 'c'){--cnt;}
if(w >= 1 && s[w - 1] == 'a' && s[w] == 'b' && w + 1 < s.size() && s[w + 1] == 'c'){--cnt;}
s[w] = c;
if(w + 2 < s.size() && s[w + 1] == 'b' && s[w + 2] == 'c'){++cnt;}
}
else if(s[w] != c && c == 'b'){
if(w >= 2 && s[w - 2] == 'a' && s[w - 1] == 'b' && s[w] == 'c'){--cnt;}
if(w + 2 < s.size() && s[w] == 'a' && s[w + 1] == 'b' && s[w + 2] == 'c'){--cnt;}
s[w] = c;
if(w >= 1 && s[w - 1] == 'a' && s[w] == 'b' && w + 1 < s.size() && s[w + 1] == 'c'){++cnt;}
}
else if(s[w] != c && c == 'c'){
if(w + 2 < s.size() && s[w] == 'a' && s[w + 1] == 'b' && s[w + 2] == 'c'){--cnt;}
if(w >= 1 && s[w - 1] == 'a' && s[w] == 'b' && w + 1 < s.size() && s[w + 1] == 'c'){--cnt;}
s[w] = c;
if(w >= 2 && s[w - 2] == 'a' && s[w - 1] == 'b' && s[w] == 'c'){++cnt;}
}
std::cout << cnt << std::endl;
}
}