fork download
  1. // G. They Are Everywhere
  2.  
  3.  
  4. #pragma GCC target("avx2")
  5. #pragma GCC optimization("O3")
  6. #pragma GCC optimization("unroll-loops")
  7.  
  8. #include<bits/stdc++.h>
  9.  
  10. using namespace std;
  11.  
  12. typedef long long ll;
  13. typedef long double ld;
  14. typedef pair<ll, ll> pll;
  15. typedef vector<bool> vb;
  16. typedef vector<int> vi;
  17. typedef vector<ll> vll;
  18. typedef vector<vi> vvi;
  19. typedef vector<vb> vvb;
  20. typedef vector<vll> vvll;
  21. typedef vector<pll> vpll;
  22. typedef vector<string> vs;
  23. typedef unordered_map<ll, ll> umll;
  24. template<class T>
  25. using pq = priority_queue<T, vector<T>, greater<T>>;
  26.  
  27. #define io \
  28.   ios_base::sync_with_stdio(false); \
  29.   cin.tie(nullptr);
  30.  
  31. void solve() {
  32. int n;
  33. cin >> n;
  34. string s;
  35. cin >> s;
  36. set<char> st;
  37. for (int i = 0; i < n; ++i) {
  38. st.insert(s[i]);
  39. }
  40. int dist = st.size();
  41. int l = 0, r = 0, count = 0, ans = INT_MAX;
  42. map<char, int> mp;
  43. while (l < n) {
  44. if (count == dist) {
  45. ans = min(ans, r-l);
  46. mp[s[l]]--;
  47. if (mp[s[l]] == 0) {
  48. count--;
  49. }
  50. l++;
  51. } else {
  52. if (r >= n) {
  53. break;
  54. }
  55. mp[s[r]]++;
  56. if (mp[s[r]] == 1) {
  57. count++;
  58. }
  59. r++;
  60. }
  61. }
  62. cout << ans;
  63. }
  64.  
  65. int main() {
  66. io;
  67. ll tests = 1;
  68. // cin >> tests;
  69. while (tests--) {
  70. solve();
  71. }
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
2147483647