fork(1) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int lli;
  4.  
  5. int solve(string s, int n)
  6. {
  7. map <char, int> m;
  8. int i = 0, j = n - 1;
  9. for (; i <= j; i++, j--)
  10. {
  11. m[s[i]] = i;
  12. m[s[j]] = j;
  13. }
  14. typedef map <char, int> :: iterator mapitr;
  15. int min = INT_MAX;
  16. int max = 0;
  17. for (mapitr it = m.begin(); it != m.end(); it++)
  18. {
  19. int cur = it -> second;
  20. if (cur < min) min = cur;
  21. if (cur > max) max = cur;
  22. }
  23. return max - min + 1;
  24. }
  25.  
  26. int main()
  27. {
  28. ios_base::sync_with_stdio(false);
  29. cin.tie(NULL);
  30.  
  31. int n;
  32. cin >> n;
  33. string s;
  34. cin >> s;
  35. cout << solve(s, n) << endl;
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 4560KB
stdin
6
aaBCCe
stdout
5