This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int N = 1e6;
string s;
int n;
vector<int> to[N];
pair<int, int> buildtree(int l, int r, int d){
if(l==r)return {1, n};
pair<int, int> ans1;
pair<int, int> ans2;
int pos = to[d].back();
to[d].pop_back();
ans1 = buildtree(l+4, pos-1, d+1);
ans2 = buildtree(pos+1, r-1, d+1);
if(s[l+2]=='x'){
pair<int, int> ret;
ret.S = max(ans1.S, ans2.S);
ret.F = max(ans1.F, ans2.F);
if(ans1.F==ans2.F)ret.F++;
return ret;
}
else{
pair<int, int> ret;
ret.S = min(ans1.S, ans2.S);
ret.F = min(ans1.F, ans2.F);
if(ans1.S==ans2.S)ret.S--;
return ret;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
int cnto=0;
for(int i=0; i< s.size(); i++){
char c = s[i];
if(c=='?')n++;
if(c=='(')cnto++;
if(c==')')cnto--;
if(c==',')to[cnto-1].push_back(i);
}
for(int i=0; i<10; i++){
reverse(to[i].begin(), to[i].end());
}
pair<int, int> ans = buildtree(0, s.size()-1, 0);
cout << ans.S-ans.F << '\n';
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:44:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0; i< s.size(); i++){
| ~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |