Codeforces Round #849 (Div. 4) 1791D. Distinct Split Solution
Problem Link : https://codeforces.com/contest/1791/problem/D
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std ;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t ;
- cin >> t ;
- while ( t-- ){
- int n ;
- cin >> n ;
- string s ;
- cin >> s ;
- std::vector<int> pre(n,0),sup(n,0);
- set<char>st ;
- for ( int i = 0 ; i < n ; i++ ){
- st.insert(s[i]) ;
- pre[i] = st.size() ;
- }
- st.clear();
- for ( int i = n-1 ; i >= 0 ; i-- ){
- st.insert(s[i]) ;
- sup[i] = st.size() ;
- }
- st.clear();
- int ans = 0 ;
- for ( int i = 0 ; i < n-1 ; i++ ){
- ans = max ( ans , sup[i+1]+pre[i]) ;
- }
- cout << ans<< endl;
- st.clear() ;
- }
- return 0 ;
- }
No comments