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. //* Count of Exactly k = (Atmost k ) - (Atmost k-1 )
  20.  
  21. int atmostK(int n, int k){
  22.  
  23. vector<int> mp(26);
  24. int size = 0;
  25.  
  26. int ans = 0;
  27. int s = 0, e = 0;
  28.  
  29. while(e<n){
  30. mp[a[e]-'a']++;
  31. if(mp[a[e]-'a'] == 1) size++;
  32.  
  33. if(size < k){
  34. ans += (e-s+1);
  35. e++;
  36. }
  37. else{
  38. while(s<=e && size > k){
  39. mp[a[s]-'a']--;
  40. if(mp[a[s] - 'a'] == 0) size--;
  41. s++;
  42. }
  43. ans += (e-s+1);
  44. e++;
  45. }
  46. }
  47.  
  48. return ans;
  49. }
  50.  
  51. int consistency1(int n, int k){
  52.  
  53. return atmostK(n, k) - atmostK(n, k-1);
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60. //* Template 1
  61.  
  62.  
  63. int consistency2(int n, int k){
  64.  
  65. vector<int> big(26), small(26);
  66. int bigSize = 0, smallSize = 0;
  67.  
  68. int ans = 0;
  69. int s = 0, e = 0;
  70. int l = 0;
  71.  
  72. while(e<n){
  73. big[a[e]-'a']++;
  74. small[a[e]-'a']++;
  75. if(big[a[e]-'a'] == 1) bigSize++;
  76. if(small[a[e]-'a'] == 1) smallSize++;
  77.  
  78. while(l<=e && smallSize >= k){
  79. small[a[l]-'a']--;
  80. if(small[a[l]-'a'] == 0) smallSize--;
  81. l++;
  82. }
  83.  
  84. if(bigSize < k){
  85. e++;
  86. }
  87. else if(bigSize == k){
  88. ans += (l-s);
  89. e++;
  90. }
  91. else{
  92. while(s<=e && bigSize > k){
  93. big[a[s]-'a']--;
  94. if(big[a[s] - 'a'] == 0) bigSize--;
  95. s++;
  96. }
  97. ans += (l-s);
  98. e++;
  99. }
  100. }
  101.  
  102. return ans;
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109. //* Template 2
  110.  
  111. int consistency3(int n, int k){
  112.  
  113. vector<int> big(26), small(26);
  114. int bigSize = 0, smallSize = 0;
  115.  
  116. int ans = 0;
  117. int s = 0, e = 0;
  118. int l = 0;
  119.  
  120. while(e<n){
  121. big[a[e]-'a']++;
  122. small[a[e]-'a']++;
  123. if(big[a[e]-'a'] == 1) bigSize++;
  124. if(small[a[e]-'a'] == 1) smallSize++;
  125.  
  126. while(l<=e && smallSize >= k){
  127. small[a[l]-'a']--;
  128. if(small[a[l]-'a'] == 0) smallSize--;
  129. l++;
  130. }
  131.  
  132. if(bigSize < k){
  133. e++;
  134. }
  135. else{
  136. while(s<=e && bigSize > k){
  137. big[a[s]-'a']--;
  138. if(big[a[s] - 'a'] == 0) bigSize--;
  139. s++;
  140. }
  141. ans += (l-s);
  142. e++;
  143. }
  144. }
  145.  
  146. return ans;
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. int practice(int n, int k){
  158.  
  159. return 0;
  160. }
  161.  
  162.  
  163.  
  164.  
  165. void solve() {
  166.  
  167. int k;
  168. cin >> k >> a;
  169. int n = a.size();
  170.  
  171. cout << consistency1(n, k) << " " << consistency2(n,k) << " " << consistency3(n,k) << endl;
  172.  
  173. // cout << consistency1(n, k) << " -> " << practice(n, k) << endl;
  174.  
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. int32_t main() {
  182. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  183.  
  184. int t = 1;
  185. cin >> t;
  186. while (t--) {
  187. solve();
  188. }
  189.  
  190. return 0;
  191. }
Success #stdin #stdout 0s 5324KB
stdin
3
2 abc
2 aba
1 aa
stdout
2 2 2
3 3 3
3 3 3