Advertisement
Plabon_dutta

E. Making Anti-Palindromes

Apr 25th, 2023
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  7.  
  8.     int testcase;
  9.     cin >> testcase;
  10.     while(testcase--) {
  11.         int n;
  12.         cin >> n;
  13.  
  14.         string s;
  15.         cin >> s;
  16.  
  17.         if (n % 2) {
  18.             cout << -1 << '\n';
  19.             continue;
  20.         }
  21.  
  22.         map <char, int> mm;
  23.         int mx = 0;
  24.         for (int i = 0; i < n; i++) {
  25.             mm[s[i]]++;
  26.             mx = max(mx, mm[s[i]]);
  27.         }
  28.  
  29.         if (mx > (n / 2)) {
  30.             cout << -1 << '\n';
  31.             continue;
  32.         }
  33.  
  34.         mm.clear();
  35.         mx = 0;
  36.         int c = 0;
  37.         for (int i = 0; i < (n / 2); i++) {
  38.             if (s[i] == s[n - i - 1]) {
  39.                 mm[s[i]]++;
  40.                 c++;
  41.                 mx = max(mx, mm[s[i]]);
  42.             }
  43.         }
  44.  
  45.         cout << max(mx, (c + 1) / 2) << '\n';
  46.     }
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement