给一个字符串,求查询的区间包含多少个长度为3且字符相同的连续字串。例如aaabbbb,查询【1,6】答案是2,查询【1,7】答案是3。从大佬那边知道这道题用前缀和做,但我还是想知道为啥我的写法只能过13.33,查了老半天,哪里逻辑有问题呢?n, k = map(int, input().split())s = input()query = [] #存储查询区间for _ in range(k): l, r = map(int, input().split()) query.append([l-1,r-1])res = [0] * kdp = [0] * nfor i in range(n): # i开头,j结尾 for j in range(i, n): if j - i dp[j] = 0 elif j - i == 2 and len(set(s[i:j+1])) == 1: # 长度为3 dp[j] = 1 else: # 长度等于3但不满足条件或长度大于3 dp[j] = dp[j-1] + (s[j] == s[j-1] and s[j] == s[j-2]) if [i,j] in query: #如果该区间需要查询 idx = query.index([i,j]) res[idx] = dp[j] print(dp)#打印结果for i in res: print(i)