Advertisement
Ahmed_Negm

Untitled

Mar 5th, 2023
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35. ll calc(string s, char c){
  36.     ll ans = 0;
  37.     for(auto&i:s) ans+=(i!=c);
  38.     return ans;
  39. }
  40.  
  41. ll min_operation(string s,char c){
  42.     if(sz(s)==1) return s[0] == c ?0:1;
  43.     ll mid = sz(s)/2;    
  44.     ll op1 = calc(s.substr(0,mid),c) + min_operation(s.substr(mid),c+1);
  45.     ll op2 = calc(s.substr(mid),c) + min_operation(s.substr(0,mid),c+1);
  46.     return min(op1,op2);
  47. }
  48.  
  49.  
  50.  
  51.  
  52. void solve(){
  53. ll n; cin>>n;
  54. string s; cin>>s;
  55. cout<<min_operation(s,'a')<<nl;
  56.  
  57.  
  58.  
  59. }
  60.  
  61. int main(){
  62.     Fast_IO();
  63. int t =1;
  64. cin>>t;
  65. while(t--){
  66. solve();
  67. }
  68. return 0;
  69. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement