nq1s788

а-хорошая строка кол-во замен

Aug 12th, 2026
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <set>
  6. #include <map>
  7.  
  8. #define fi first
  9. #define se second
  10. #define pb push_back
  11.  
  12. typedef long long ll;
  13.  
  14. using namespace std;
  15.  
  16. string s;
  17.  
  18. int cnt(int l, int r, char c) {
  19.     if (r - l == 1) {
  20.         return (s[l] != c);
  21.     }
  22.     int m = (l + r) / 2;
  23.     int cnt_left = 0;
  24.     for (int i = l; i < m; i++) {
  25.         if (s[i] != c) cnt_left++;
  26.     }
  27.     int cnt_right = 0;
  28.     for (int i = m; i < r; i++) {
  29.         if (s[i] != c) cnt_right++;
  30.     }
  31.     return min(cnt_left + cnt(m, r, c+1), cnt_right + cnt(l, m, c+1));
  32. }
  33.  
  34. int main() {
  35.     cin >> s;
  36.     cout << cnt(0, (int)s.size(), 'a');
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment