当前位置:网站首页>cf:D. Black and White Stripe【连续k个中最少的个数 + 滑动窗口】

cf:D. Black and White Stripe【连续k个中最少的个数 + 滑动窗口】

2022-06-11 18:37:00 白速龙王的回眸

在这里插入图片描述

分析

每次l和r前进一个
分析头和尾的元素,来得到当前窗口中最少的个数即可

Ac code

for t in range(int(input())):
    #print(str(t) + ':')
    n, k = list(map(int, input().split()))
    s = input()

    cntW = 0
    ans = 0xffffffff
    for i in range(k):
        if s[i] == 'W':
            cntW += 1
    ans = min(ans, cntW)

    for i in range(k, n):
        if s[i] == 'W' and s[i - k] == 'W':
            pass
        elif s[i] == 'W' and s[i - k] == 'B':
            cntW += 1
        elif s[i] == 'B' and s[i - k] == 'W':
            cntW -= 1
        elif s[i] == 'B' and s[i - k] == 'B':
            pass
        ans = min(ans, cntW)
    print(ans)

总结

思维转换 + 滑动窗口

原网站

版权声明
本文为[白速龙王的回眸]所创,转载请带上原文链接,感谢
https://bridge-killer.blog.csdn.net/article/details/125237056