Advertisement
Anwar_Rizk

E. Maximum distinct

Aug 10th, 2022
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/tree_policy.hpp>
  3. #include <ext/pb_ds/assoc_container.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8. #define EPS 1e-9
  9. #define PI acos(-1)
  10. #define ll long long
  11. #define Mod 1'000'000'007
  12. #define INF 2'000'000'000
  13. #define sz(x) int(x.size())
  14. #define all(s) s.begin(), s.end()
  15. #define rall(s) s.rbegin(), s.rend()
  16. #define Num_of_Digits(n) ((int)log10(n)+1)
  17. #define to_decimal(bin) stoll(bin, nullptr, 2)
  18. #define fixed(n) cout << fixed << setprecision(n)
  19. #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  20. #define cout(st) for(auto& i : st) cout << i << " "; cout << "\n"
  21. #define cout_map(mp) for(auto& [f, s] : mp) cout << f << " : " << s << "\n"
  22. #define matrix(grid, n, m) vector < vector <int> > grid(n, vector <int> (m));
  23. #define cout_2d(grid) for(auto& v : grid){for(auto& x : v) cout << x << " "; cout << "\n";}
  24. #define cin_2d(grid, n, m) for(int i=0; i<n; i++) for(int j=0; j<m && cin >> grid[i][j]; j++);
  25. #define ordered_set tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>
  26. #define multi_ordered_set tree<int, null_type, greater_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  27.  
  28. template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
  29.     for (auto &x: v) in >> x;
  30.     return in;
  31. }
  32.  
  33. template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
  34.     for (const T &x: v) out << x << ' ';
  35.     return out;
  36. }
  37.  
  38. void Anwar_Rizk(){
  39.   ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  40.   #ifndef ONLINE_JUDGE
  41.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  42.   #endif
  43. }
  44.  
  45. void solve(){
  46.     int n, k, ans = 0, cnt = 0;
  47.     string s;
  48.     cin >> n >> k >> s;
  49.     map < char, int > occ;
  50.     for(int i = 0; i < k; i++){
  51.         occ[s[i]]++;
  52.         if(occ[s[i]] == 1) cnt++;
  53.     }
  54.     ans = cnt;
  55.     int l = 0, r = k;
  56.     while(r < n){
  57.         occ[s[r]]++;
  58.         if(occ[s[r]] == 1) cnt++;
  59.         occ[s[l]]--;
  60.         if(occ[s[l]] == 0) cnt--;
  61.         ans = max(ans, cnt);
  62.         l++, r++;
  63.     }
  64.     cout << ans << "\n";
  65. }
  66.  
  67. int main()
  68. {   Anwar_Rizk();
  69.  
  70.     int t = 1;
  71.     //cin >> t;
  72.     while(t--){
  73.       solve();
  74.     }
  75.  
  76.   return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement