Say No to Palindromes (Codeforces 1555D)
Tags: Others, strings

Let's call the string beautiful if it does not contain a substring of length at least $$$2$$$, which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first $$$3$$$ letters of the Latin alphabet (in lowercase).You are given a string $$$s$$$ of length $$$n$$$, each character of the string is one of the first $$$3$$$ letters of the Latin alphabet (in lowercase).You have to answer $$$m$$$ queries — calculate the cost of the substring of the string $$$s$$$ from $$$l_i$$$-th to $$$r_i$$$-th position, inclusive.

Input: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$ and the number of queries.The second line contains the string $$$s$$$, it consists of $$$n$$$ characters, each character one of the first $$$3$$$ Latin letters.The following $$$m$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — parameters of the $$$i$$$-th query.

Output: For each query, print a single integer — the cost of the substring of the string $$$s$$$ from $$$l_i$$$-th to $$$r_i$$$-th position, inclusive.

Note: Consider the queries of the example test.  in the first query, the substring is baa, which can be changed to bac in one operation;  in the second query, the substring is baacb, which can be changed to cbacb in two operations;  in the third query, the substring is cb, which can be left unchanged;  in the fourth query, the substring is aa, which can be changed to ba in one operation. 

Sample Inputs:
5 4
baacb
1 3
1 5
4 5
2 3

Sample Outputs:
1
2
0
1

Solution (Python):
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

def MI():
    return map(int, sys.stdin.buffer.readline().split())

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()

def li():
    return [int(i) for i in input().split()]

def lli(rows):
    return [li() for _ in range(rows)]

def si():
    return input()

def ii():
    return int(input())

def ins():
    return input().split()

def solve():
    (n, m) = LI()
    s = SI()
    x = n % 3
    p1 = 'abc' * (n // 3) + 'abc'[:x]
    p2 = 'bca' * (n // 3) + 'bca'[:x]
    p3 = 'cab' * (n // 3) + 'cab'[:x]
    p4 = 'acb' * (n // 3) + 'acb'[:x]
    p5 = 'cba' * (n // 3) + 'cba'[:x]
    p6 = 'bac' * (n // 3) + 'bac'[:x]
    tp1 = [0]
    count = 0
    for i in range(n):
        if p1[i] != s[i]:
            count += 1
            tp1.append(count)
        else:
            tp1.append(count)
    tp2 = [0]
    count = 0
    for i in range(n):
        if p2[i] != s[i]:
            count += 1
            tp2.append(count)
        else:
            tp2.append(count)
    tp3 = [0]
    count = 0
    for i in range(n):
        if p3[i] != s[i]:
            count += 1
            tp3.append(count)
        else:
            tp3.append(count)
    tp4 = [0]
    count = 0
    for i in range(n):
        if p4[i] != s[i]:
            count += 1
            tp4.append(count)
        else:
            tp4.append(count)
    tp5 = [0]
    count = 0
    for i in range(n):
        if p5[i] != s[i]:
            count += 1
            tp5.append(count)
        else:
            tp5.append(count)
    tp6 = [0]
    count = 0
    for i in range(n):
        if p6[i] != s[i]:
            count += 1
            tp6.append(count)
        else:
            tp6.append(count)
    for i in range(m):
        (l, r) = LI()
        print(min(tp1[r] - tp1[l - 1], tp2[r] - tp2[l - 1], tp3[r] - tp3[l - 1], tp4[r] - tp4[l - 1], tp5[r] - tp5[l - 1], tp6[r] - tp6[l - 1]))
    return

def main():
    solve()
if __name__ == '__main__':
    main()

Solution (Python):
from itertools import *
import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
s = input()
ps = [list(accumulate((c1 != c2 for (c1, c2) in zip(cycle(pm), s)), initial=0)) for pm in permutations('abc')]
for _ in range(m):
    (l, r) = map(int, input().split())
    print(min((p[r] - p[l - 1] for p in ps)))

Solution (Python):
from itertools import *
import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
s = input()
ps = [list(accumulate((c1 != c2 for (c1, c2) in zip(cycle(pm), s)), initial=0)) for pm in permutations('abc')]
for _ in range(m):
    (l, r) = map(int, input().split())
    print(min((p[r] - p[l - 1] for p in ps)))