William the Vigilant (Codeforces 1609B)
Tags: Implement, strings

  Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Input: The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively.The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c".Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c".

Output: For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring.

Note: Let's consider the state of the string after each query:   $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed.

Sample Inputs:
9 10
abcabcabc
1 a
1 b
2 c
3 a
4 b
5 c
8 a
9 b
1 c
4 a

Sample Outputs:
3
2
2
2
1
2
1
1
1
0

Solution (Python):
import sys
input = sys.stdin.readline
print = sys.stdout.write
(n, q) = map(int, input().split())
s = input()
pos_abc = set((i for i in range(n - 2) if s[i:i + 3] == 'abc'))
s = list(s)
ans = []
for _ in range(q):
    d = input().split()
    i = int(d[0]) - 1
    c = d[1]
    if s[i] != c:
        if s[i] == 'a':
            pos_abc.discard(i)
        elif s[i] == 'b':
            pos_abc.discard(i - 1)
        else:
            pos_abc.discard(i - 2)
        s[i] = c
        if c == 'a':
            if s[i:i + 3] == ['a', 'b', 'c']:
                pos_abc.add(i)
        elif c == 'b':
            if s[i - 1:i + 2] == ['a', 'b', 'c']:
                pos_abc.add(i - 1)
        elif s[i - 2:i + 1] == ['a', 'b', 'c']:
            pos_abc.add(i - 2)
    ans.append(len(pos_abc))
print('\n'.join(map(str, ans)))

Solution (Python):
import sys
import math

def main():
    input = sys.stdin.readline
    print = sys.stdout.write
    (n, q) = map(int, input().split())
    s = input()
    s = list(s)
    count = 0
    for i in range(n - 2):
        if s[i] == 'a':
            if s[i + 1] == 'b' and s[i + 2] == 'c':
                count += 1
    for j in [0] * q:
        query = input().split()
        (i, c) = (int(query[0]) - 1, query[1])
        if c == 'a':
            if s[i] != 'a':
                if s[i] == 'b':
                    if (i + 1 < n and s[i + 1] == 'b') and (i + 2 < n and s[i + 2] == 'c'):
                        count += 1
                    if (i - 1 >= 0 and s[i - 1] == 'a') and (i + 1 < n and s[i + 1] == 'c'):
                        count -= 1
                else:
                    if (i + 1 < n and s[i + 1] == 'b') and (i + 2 < n and s[i + 2] == 'c'):
                        count += 1
                    if (i - 2 >= 0 and s[i - 2] == 'a') and (i - 1 >= 0 and s[i - 1] == 'b'):
                        count -= 1
            s[i] = 'a'
        elif c == 'b':
            if s[i] != 'b':
                if s[i] == 'a':
                    if (i - 1 >= 0 and s[i - 1] == 'a') and (i + 1 < n and s[i + 1] == 'c'):
                        count += 1
                    if (i + 1 < n and s[i + 1] == 'b') and (i + 2 < n and s[i + 2] == 'c'):
                        count -= 1
                else:
                    if (i - 1 >= 0 and s[i - 1] == 'a') and (i + 1 < n and s[i + 1] == 'c'):
                        count += 1
                    if (i - 2 >= 0 and s[i - 2] == 'a') and (i - 1 >= 0 and s[i - 1] == 'b'):
                        count -= 1
            s[i] = 'b'
        else:
            if s[i] != 'c':
                if s[i] == 'a':
                    if (i - 2 >= 0 and s[i - 2] == 'a') and (i - 1 >= 0 and s[i - 1] == 'b'):
                        count += 1
                    if (i + 1 < n and s[i + 1] == 'b') and (i + 2 < n and s[i + 2] == 'c'):
                        count -= 1
                else:
                    if (i - 1 >= 0 and s[i - 1] == 'a') and (i + 1 < n and s[i + 1] == 'c'):
                        count -= 1
                    if (i - 2 >= 0 and s[i - 2] == 'a') and (i - 1 >= 0 and s[i - 1] == 'b'):
                        count += 1
            s[i] = 'c'
        print(str(count) + '\n')
if __name__ == '__main__':
    main()

Solution (Python):
import sys
import bisect
import math
from collections import deque
input = lambda : sys.stdin.readline().strip()
mod = 10 ** 9 + 7

def solve():
    (n, q) = map(int, input().split())
    s = input()
    cnt = 0
    for i in range(n):
        if i + 2 < n and s[i:i + 3] == 'abc':
            cnt += 1
    s = list(s)
    ans = ''
    for _q in range(q):
        (pos, char) = input().split()
        pos = int(pos)
        pos -= 1
        if s[pos] != char:
            if s[pos] == 'a' and pos + 2 < n and (s[pos:pos + 3] == ['a', 'b', 'c']):
                cnt -= 1
            elif s[pos] == 'b' and 0 < pos < n - 1 and (s[pos - 1:pos + 2] == ['a', 'b', 'c']):
                cnt -= 1
            elif s[pos] == 'c' and pos > 1 and (s[pos - 2:pos + 1] == ['a', 'b', 'c']):
                cnt -= 1
            s[pos] = char
            if s[pos] == 'a' and pos + 2 < n and (s[pos:pos + 3] == ['a', 'b', 'c']):
                cnt += 1
            elif s[pos] == 'b' and 0 < pos < n - 1 and (s[pos - 1:pos + 2] == ['a', 'b', 'c']):
                cnt += 1
            elif s[pos] == 'c' and pos > 1 and (s[pos - 2:pos + 1] == ['a', 'b', 'c']):
                cnt += 1
        ans += str(cnt) + '\n'
    return ans[:-1]
if __name__ == '__main__':
    t = 1
    for _ in range(t):
        print(solve())