python 算法竞赛模板 头

# import collections
# import random
# import math
# from collections import defaultdict
# import itertools
# from sys import stdin, stdout
import sys
# import operator
# from decimal import Decimal

# sys.setrecursionlimit(10**6)

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
    # print(p1)
    # print(p2)
    # print(p3)
    # print(p4)
    # print(p5)
    # print(p6)
    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)

    # print(tp1)
    # print(tp2)
    # print(tp3)
    # print(tp4)
    # print(tp5)
    # print(tp6)

    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():
    #for _ in range(II()):
    solve()


    #     z += str(ans) + '\n'
    # print(len(ans), ' '.join(map(str, ans)), sep='\n')
    # stdout.write(z)


# for interactive problems
# print("? {} {}".format(l,m), flush=True)
# or print this after each print statement
# sys.stdout.flush()


if __name__ == "__main__":
    main()
全部评论

相关推荐

10-14 13:25
已编辑
门头沟学院 C++
点赞 评论 收藏
分享
11-28 17:58
门头沟学院 Java
美团 JAVA开发 n×15.5
牛客786276759号:百度现在晋升很难的 而且云这块的业务没美团好 你看百度股价都跌成啥样了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务