fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5.  
  6.  
  7. const int M = 1000000007;
  8. const int N = 3e5+9;
  9. const int INF = 2e9+1;
  10. const int MAXN = 100000;
  11. const int LINF = 2000000000000000001;
  12.  
  13. //_ ***************************** START Below *******************************
  14.  
  15.  
  16.  
  17. string a;
  18.  
  19.  
  20.  
  21.  
  22. //* Template 1
  23.  
  24. int consistency1(int n, int k){
  25.  
  26. int ans = -1;
  27. int s = 0, e = 0;
  28. unordered_map<int,int> mp;
  29.  
  30. while(e<n){
  31. mp[a[e]]++;
  32.  
  33. if(mp.size() < k){
  34. e++;
  35. }
  36. else {
  37. while(s<=e && mp.size() > k) {
  38. mp[a[s]]--;
  39. if(mp[a[s]] == 0) mp.erase(a[s]);
  40. s++;
  41. }
  42.  
  43. if(mp.size() == k) ans = max(ans, e-s+1);
  44. e++;
  45. }
  46. }
  47. return ans;
  48.  
  49. }
  50.  
  51.  
  52.  
  53.  
  54. //* Template 2
  55.  
  56. int consistency2(int n, int k){
  57.  
  58. int ans = -1;
  59. int s = 0, e = 0;
  60. unordered_map<int,int> mp;
  61.  
  62. while(e<n){
  63. mp[a[e]]++;
  64.  
  65. while(s<=e && mp.size() > k) {
  66. mp[a[s]]--;
  67. if(mp[a[s]] == 0) mp.erase(a[s]);
  68. s++;
  69. }
  70.  
  71.  
  72. if(mp.size() == k) ans = max(ans, e-s+1);
  73. e++;
  74. }
  75.  
  76. return ans;
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. int practice(int n, int k){
  98.  
  99. return 0;
  100. }
  101.  
  102.  
  103.  
  104.  
  105. void solve() {
  106.  
  107. int k;
  108. cin >> k >> a;
  109. int n = a.size();
  110.  
  111. cout << consistency1(n, k) << " " << consistency2(n,k) << endl;
  112.  
  113. // cout << consistency1(n, k) << " -> " << practice(n, k) << endl;
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121. int32_t main() {
  122. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  123.  
  124. int t = 1;
  125. cin >> t;
  126. while (t--) {
  127. solve();
  128. }
  129.  
  130. return 0;
  131. }
Success #stdin #stdout 0s 5320KB
stdin
3
3 aabacbebebe
2 aaaa
2 aabaaab
stdout
7 7
-1 -1
7 7