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()
全部评论

相关推荐

Tom哥981:这份简历是“大一新生硬凹资深后端”的典型反面教材,槽点离谱到能让面试官直接笑出声: ### 1. 「年龄+入学时间」和项目复杂度完全脱节,可信度直接归0 你2024年7月才入学(现在刚读了1年多),19岁的大一新生,能把Vue3+Spring Boot+ShardingSphere+K8s+AI这些技术全塞进两个项目里?别说实际开发,光把这些技术的文档看完都得半年——这不是“能力强”,是“把招聘JD里的技术词全抄过来造假”,明摆着没碰过实际代码
点赞 评论 收藏
分享
2025-12-28 22:19
门头沟学院 Java
不敢追165女神:简历写得毫无特点,你说你要是大二或者大三找寒假实习到暑期实习这段时间,你的简历还能约到面试。但是你是研究生哥,面试官不会因为你是研究生而降低要求,反而会觉得你是研究生才学了这么一点?为什么我不找个同阶段的本科生?
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务