fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void test_case() {
  4. int n;
  5. cin >> n;
  6. string s;
  7. cin >> s;
  8. set<char> ss;
  9. for (int i = 0; i < n; ++i) {
  10. ss.insert(s[i]);
  11. }
  12. int unique = (int)ss.size();
  13. vector<bool> vis(26, false);
  14. vector<int> num_unique(n+1);
  15. num_unique[0] = 0;
  16. for (int i = 0; i < n; ++i) {
  17. if (!vis[s[i]]) {
  18. num_unique[i+1] = num_unique[i] + 1;
  19. vis[s[i]] = true;
  20. }
  21. else {
  22. num_unique[i+1] = num_unique[i];
  23. }
  24. }
  25. int i = 1, j = n, maxi = 0, ans = 0;
  26. while (i <= j) {
  27. int dist = num_unique[j]-num_unique[i];
  28. if (dist >= maxi) {
  29. maxi = dist;
  30. ans = j-i+1;
  31. }
  32. if (i+1 < n+1 && num_unique[i+1] == num_unique[i]) {
  33. ++i;
  34. }
  35. else if (j-1 > i && num_unique[j-1] == num_unique[j]) {
  36. --j;
  37. }
  38. else {
  39. ++i;
  40. }
  41. }
  42. cout << ans << '\n';
  43. }
  44. int main(int argc, char const *argv[]) {
  45. ios_base::sync_with_stdio(false), cin.tie(nullptr);
  46. int tc = 1;
  47. // cin >> tc;
  48. while (tc--) {
  49. test_case();
  50. }
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0.01s 5464KB
stdin
Standard input is empty
stdout
0