传送门
思路:
分别记录每一个位置前面有多少不同字符,后面有多少不同字符,然后取 max (前面 + 后面).
参考代码:
void solve() {
int n, ans{};
std::string s;
std::cin >> n >> s;
std::vector<int> cnt(n + 1);
std::set<char> hash;
for (int i = n - 1; i >= 0; i--) {
hash.emplace(s[i]);
cnt[i] = hash.size();
}
//cnt[i] 记录位置 i 及后面所有字符中不同字符的个数
hash.clear();
//清空容器
for (int i = 0; i < n; i++) {
hash.emplace(s[i]);
ans = std::max(ans, (int) hash.size() + cnt[i + 1]);
//前面所有字符中不同字符的数量 + 后面所有... 取 max
}
std::cout << ans << '\n';
}