fork(1) download
  1. #ifndef LOCAL
  2. // #pragma GCC optimize("Ofast,unroll-loops")
  3. #endif
  4. #include <climits>
  5. #include <unordered_map>
  6. #include <random>
  7. #include <chrono>
  8. #include <numeric>
  9. #include <iostream>
  10. #include <vector>
  11. #include <algorithm>
  12. #include <map>
  13. #include <queue>
  14. #include <deque>
  15. #include <stack>
  16. #include <functional>
  17. #include <bitset>
  18. #include <string>
  19. #include <sstream>
  20. #include <fstream>
  21. #include <iomanip>
  22. #include <cmath>
  23. #include <cassert>
  24. #include <list>
  25. #include <forward_list>
  26. #include <set>
  27. #include <unordered_set>
  28. #include <cstdint>
  29. #include <ext/pb_ds/assoc_container.hpp>
  30. #ifndef LOCAL
  31. // #pragma GCC target("avx,avx2,fma")
  32. #endif
  33.  
  34. using namespace std;
  35. using namespace __gnu_pbds;
  36.  
  37. using ll = long long;
  38. using ull = unsigned long long;
  39. using ld = long double;
  40.  
  41. #define all(x) begin(x), end(x)
  42. #define isz(x) ((int)size(x))
  43. #define X first
  44. #define Y second
  45.  
  46. bool is_pal(const string& s, int l, int r) {
  47. vector<int> cnt(26);
  48. while (l <= r) {
  49. cnt[s[l++] - 'a'] ^= 1;
  50. }
  51. return count(all(cnt), 1) < 2;
  52. }
  53.  
  54. int check(const string& s, int l, int r) {
  55. int ans = 0;
  56. for (int i = l; i <= r; ++i) {
  57. for (int j = l; j <= i; ++j) {
  58. if (!is_pal(s, j, i))
  59. ans = max(ans, i - j + 1);
  60. }
  61. }
  62. return ans;
  63. }
  64.  
  65. void solve() {
  66. int n;
  67. cin >> n;
  68. string s;
  69. cin >> s;
  70. int q;
  71. cin >> q;
  72. while (q --> 0) {
  73. int l, r;
  74. cin >> l >> r;
  75. cout << check(s, l - 1, r - 1) << endl;
  76. }
  77. }
  78.  
  79. signed main() {
  80. #ifdef LOCAL
  81. freopen("in.txt", "r", stdin);
  82. // freopen("out.txt", "w", stdout);
  83. // freopen("in.txt", "w", stdout); /* для стресс-ввода */
  84. #endif
  85. // cin.tie(0)->sync_with_stdio(0);
  86. int t = 1;
  87. cin >> t;
  88. while (t --> 0) {
  89. solve();
  90. }
  91. }
Success #stdin #stdout 0.01s 5292KB
stdin
1
60
aabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabb
1 1 60
stdout
58