Josif_tepe

Untitled

Dec 17th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <cstring>
  6. #include <random>
  7. #include <cstdlib>
  8. #include <set>
  9. #include <map>
  10. using namespace std;
  11. typedef long long ll;
  12. const int maxn = 100 + 10;
  13. const ll INF = 2e17;
  14.  
  15. int brute_force(int n, int k, string s) {
  16.     for(int i = 0; i < n; i++) {
  17.         vector<int> c(26, 0);
  18.         int cnt = 0;
  19.         for(int j = i; j < n; j++) {
  20.             if(s[j] != '?') {
  21.                 c[s[j] - 'a']++;
  22.             }
  23.             else {
  24.                 cnt++;
  25.             }
  26.            
  27.             int tmp = cnt;
  28.             bool ok = true;
  29.             for(int l = 0; l < 26; l++) {
  30.                 if(c[l] < k) {
  31.                     if(tmp >= k - c[l]) {
  32.                         tmp -= (k - c[l]);
  33.                     }
  34.                     else {
  35.                         ok = false;
  36.                         break;
  37.                     }
  38.                 }
  39.                 else if(c[l] > k) {
  40.                     ok = false;
  41.                     break;
  42.                 }
  43.             }
  44.             if(ok) {
  45.                 return i + 1;
  46.             }
  47.         }
  48.     }
  49.     return -1;
  50. }
  51.  
  52. int real_solution(int n, int k, string s) {
  53.     multiset<int> ms;
  54.     for(int i = 0; i < 26; i++) {
  55.         ms.insert(0);
  56.     }
  57.     vector<int> cnt(26, 0);
  58.     int i = 0, j = 0;
  59.     int question_marks = 0;
  60.     int chars = 0;
  61.     while(j < n) {
  62.         if(*ms.begin() <= k and *ms.rbegin() <= k) {
  63.             if(s[j] == '?') {
  64.                 question_marks++;
  65.             }
  66.             else {
  67.                 chars++;
  68.                 ms.erase(ms.find(cnt[s[j] - 'a']));
  69.                 cnt[s[j] - 'a']++;
  70.                 ms.insert(cnt[s[j] - 'a']);
  71.             }
  72.             j++;
  73.             if(chars + question_marks == 26 * k) {
  74.                 return i + 1;
  75.             }
  76.         }
  77.         else {
  78.             if(s[i] == '?') {
  79.                 question_marks--;
  80.             }
  81.             else {
  82.                 chars--;
  83.                 ms.erase(ms.find(cnt[s[i] - 'a']));
  84.                 cnt[s[i] - 'a']--;
  85.                 ms.insert(cnt[s[i] - 'a']);
  86.             }
  87.             i++;
  88.             if(chars + question_marks == 26 * k) {
  89.                 return i;
  90.             }
  91.         }
  92.     }
  93.     return -1;
  94. }
  95.  
  96. int rand_int(int A, int B) {
  97.     return A + rand() % (B - A + 1);
  98. }
  99. int main() {
  100.        
  101.     srand(time(nullptr));
  102.    
  103.     for(int i = 0; i < 1000; i++) {
  104.         int n = rand_int(1, 100);
  105.         int k = rand_int(1, 5);
  106.         string s = "";
  107.        
  108.         for(int j = 0; j < n; j++) {
  109.             int c = rand_int(0, 26);
  110.             if(c == 26) {
  111.                 s += "?";
  112.             }
  113.             else {
  114.                 s += (c + 'a');
  115.             }
  116.         }
  117.        
  118.        
  119.         if(brute_force(n, k, s) == real_solution(n, k, s)) {
  120.             cout << "Test case #" << i << " passed!" << endl;
  121.         }
  122.         else {
  123.             cout << "FAILURE: " << endl;
  124.             cout << n << " " << k << endl;
  125.             cout << s << endl;
  126.         }
  127.     }
  128.    
  129.    
  130.     return 0;
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment