/*hdu6103[尺取法] 2017多校6*/ #include <bits/stdc++.h> using namespace std; int T, m; char str[20005]; void solve() { int ans = 0; int n = strlen(str); for (int i = 0; i < n; i++) { int l = 0, r = 0, p1 = i, p2 = i + 1, cost = 0; while (p1 - r >= 0 && p2 + r < n) { if (cost + abs(str[p1 - r] - str[p2 + r]) <= m) { cost += abs(str[p1 - r] - str[p2 + r]); ans = max(++r - l, ans); } else { cost -= abs(str[p1 - l] - str[p2 + l]); l++; } } l = r = cost = 0, p1 = i - 1, p2 = i + 1; while (p1 - r >= 0 && p2 + r < n) { if (cost + abs(str[p1 - r] - str[p2 + r]) <= m) { cost += abs(str[p1 - r] - str[p2 + r]); ans = max(++r - l, ans); } else { cost -= abs(str[p1 - l] - str[p2 + l]); l++; } } } printf("%d\n", ans); } int main() { scanf("%d", &T); while (T--) { scanf("%d%s", &m, str); solve(); } return 0; }
时间: 2024-09-30 00:27:39