我的错解, 会溢出
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
if k == 0:
print('YES')
else:
if n % 2 == 0:
i = 0
while s[n//2-i-1] != s[n//2+i]:
i += 1
if s[0:n//2-i] == s[n:n//2+i-1:-1] and n//2-i >= k:
print('YES')
else:
print('NO')
else:
i = 1
while s[n//2-i] != s[n//2+i]:
i += 1
if s[0:n//2-i+1] == s[n:n//2+i-1:-1] and n//2-i+1 >= k:
print('YES')
else:
print('NO')
别人的优解
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
if n < 2 * k + 1: # 防止a[k+1]没有地方放的情况
print('no')
elif k == 0: # 如果k等于零就直接输出yes
print('yes')
else:
flag = True
for i in range(k): # 这步很妙,直接从两端记k步, 省去了比较
if s[i] != s[-i-1]: # 这步直接省去了奇数和偶数的讨论, 也完全不会溢出
flag = False
break
if flag:
print('yes')
else:
print('no')