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. //* Insufficient window => Expand window
  33. if(mp.size() < k){
  34. e++;
  35. }
  36. //* Valid window => Compute Result + Expand window
  37. else if(mp.size() == k){
  38. ans = max(ans, e-s+1);
  39. e++;
  40. }
  41. else {
  42. //* Invalid window => Shrink window
  43. while(s<=e && mp.size() > k) {
  44. mp[a[s]]--;
  45. if(mp[a[s]] == 0) mp.erase(a[s]);
  46. s++;
  47. }
  48.  
  49. //* Valid window => Compute Result + Expand window
  50. ans = max(ans, e-s+1);
  51. e++;
  52. }
  53. }
  54. return ans;
  55.  
  56. }
  57.  
  58.  
  59.  
  60.  
  61. //* Template 2
  62.  
  63. int consistency2(int n, int k){
  64.  
  65. int ans = -1;
  66. int s = 0, e = 0;
  67. unordered_map<int,int> mp;
  68.  
  69. while(e<n){
  70. mp[a[e]]++;
  71. //* Insufficient window => Expand window
  72. if(mp.size() < k){
  73. e++;
  74. }
  75. else {
  76. //* Invalid window => Shrink window
  77. while(s<=e && mp.size() > k) {
  78. mp[a[s]]--;
  79. if(mp[a[s]] == 0) mp.erase(a[s]);
  80. s++;
  81. }
  82.  
  83. //* Valid window => Compute Result + Expand window
  84. ans = max(ans, e-s+1);
  85. e++;
  86. }
  87. }
  88. return ans;
  89.  
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. int practice(int n, int k){
  101.  
  102. return 0;
  103. }
  104.  
  105.  
  106.  
  107.  
  108. void solve() {
  109.  
  110. int k;
  111. cin >> k >> a;
  112. int n = a.size();
  113.  
  114. cout << consistency1(n, k) << " " << consistency2(n,k) << endl;
  115.  
  116. // cout << consistency1(n, k) << " -> " << practice(n, k) << endl;
  117.  
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124. int32_t main() {
  125. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  126.  
  127. int t = 1;
  128. cin >> t;
  129. while (t--) {
  130. solve();
  131. }
  132.  
  133. return 0;
  134. }
Success #stdin #stdout 0s 5316KB
stdin
3
3 aabacbebebe
2 aaaa
2 aabaaab
stdout
7 7
-1 -1
7 7