当前位置:网站首页>Cf:d. black and white stripe

Cf:d. black and white stripe

2022-06-11 18:47:00 Review of the white speed Dragon King

 Insert picture description here

analysis

Every time l and r Forward one
Analyze the elements of the head and tail , To get the minimum number in the current window

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)

summary

Thinking Shifts + The sliding window

原网站

版权声明
本文为[Review of the white speed Dragon King]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111836366885.html