fork download
  1. #include <iostream>
  2. #include <unordered_set>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main() {
  7. ios::sync_with_stdio(false);
  8. cin.tie(nullptr);
  9.  
  10. int t;
  11. cin >> t;
  12.  
  13. while (t--) {
  14. int n;
  15. cin >> n;
  16. string s;
  17. cin >> s;
  18.  
  19. unordered_set<char> all_chars(s.begin(), s.end());
  20. unordered_set<char> left_chars;
  21. int max_f = 0;
  22.  
  23. for (int i = 0; i < n - 1; ++i) {
  24. left_chars.insert(s[i]);
  25. all_chars.erase(s[i]);
  26. max_f = max(max_f, (int)left_chars.size() + (int)all_chars.size());
  27. }
  28.  
  29. cout << max_f << "\n";
  30. }
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.02s 25772KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <unordered_set>
#include <string>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;
        string s;
        cin >> s;

        unordered_set<char> all_chars(s.begin(), s.end());
        unordered_set<char> left_chars;
        int max_f = 0;

        for (int i = 0; i < n - 1; ++i) {
            left_chars.insert(s[i]);
            all_chars.erase(s[i]);
            max_f = max(max_f, (int)left_chars.size() + (int)all_chars.size());
        }

        cout << max_f << "\n";
    }

    return 0;
}