第一行两个整数 n , m (1<=m<=n<=50000),第二行为长度为n且只包含’a’和’b’的字符串s。
输出在操作次数不超过 m 的情况下,能够得到的 最大连续 全’a’子串或全’b’子串的长度。
8 1 aabaabaa
5
把第一个 'b' 或者第二个 'b' 置成 'a',可得到长度为 5 的全 'a' 子串。
while True: try: line = list(map(int, input().split())) n, m = line[0], line[1] s = input() i, j = 0, 0 countA, countB = 0, 0 if s[0] == "a": countA += 1 else: countB += 1 res = 0 while i <= j and j < n - 1: if countA <= m or countB <= m: res = max(res, j-i+1) j += 1 if s[j] == "a": countA += 1 else: countB += 1 else: i += 1 if s[i-1] == "a": countA -= 1 else: countB -= 1 print(res) except: break
## 19:45 -- 20:27 n, m = [int(x) for x in input().split()] s = input() max_a = 0 max_b = 0 a_index = [] b_index = [] for i in range(n): if s[i] == 'a': a_index.append(i) else : b_index.append(i) if len(a_index) <= m or len(b_index) <= m : print(n) else : for i in range(len(a_index) - m): if i == 0: temp = a_index[m] if temp > max_b : max_b = temp elif i == len(a_index) - m : temp = n - a_index[i] if temp > max_b : max_b = temp print(i) else : temp = a_index[m+i] - a_index[i-1] - 1 if temp > max_b : max_b = temp for i in range(len(b_index) - m): if i == 0: temp = b_index[m] if temp > max_a : max_a = temp elif i == len(b_index) - m : temp = n - b_index[i] if temp > max_a : max_a = temp else : temp = b_index[m+i] - b_index[i-1] - 1 if temp > max_a : max_a = temp print(max(max_a, max_b))