SMU Spring 2023 Trial Contest Round 1(6/8)
A. Prepend and Append
Prepend and Append
只需考虑给定字符串两端是否符合10或01即可,双指针从两端模拟即可。
#include <iostream>
using namespace std;
void solution(){
int n;cin >> n;
string s;cin >> s;
int l = 0,r = n - 1;
while (l < r){
if ((s[l] == '1' && s[r] == '0') || (s[l] == '0' && s[r] == '1')){
l++,r--;
}else
break;
}
cout << r - l + 1 << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int _ = 1;cin >>_;
while (_--)
solution();
}
B. Distinct Split
Distinct Split
考虑从正逆做两次前缀和统计不同字符即可
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
const int N = 2e5 + 7;
using namespace std;
int pos[N],nev[N];
char a[N];
void solution(){
int n;cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n + 5; ++i)pos[i] = 0,nev[i] = 0;
set<int> sv;
for (int i = 1; i <= n; ++i){
sv.insert(a[i]);
if (sv.empty() || sv.size() > pos[i - 1]){
pos[i] = pos[i - 1] + 1;
}else
pos[i] = pos[i - 1];
}
sv.clear();
for (int i = n; i >= 1; --i){
sv.insert(a[i]);
if (sv.empty() || sv.size() > nev[i + 1]){
nev[i] = nev[i + 1] + 1;
}else
nev[i] = nev[i + 1];
}
int res = 0;
for (int i = 1;i <= n;i++){
res = max(res,pos[i] + nev[i + 1]);
}
cout << res << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int _ = 1;cin >>_;
while (_--)