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;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
string s;
int nxt[7000000];
/*corresponding , to each (), to split the expression into two
Main obs : all reachable values fall within a continuous interval
range returns 2 values {A,B}
A -> A smallest values cannot be reached
B -> B largest values cannot be reached
*/
ii range(int L, int R){
if (L==R) return {0,0}; // ?
ii val1 = range(L+4,nxt[L+3]-1);
ii val2 = range(nxt[L+3]+1,R-1);
if (s[L+2]=='x'){
//max
return {val1.fi+val2.fi+1, min(val1.se, val2.se)};
}
//min
return {min(val1.fi, val2.fi), val1.se+val2.se+1};
}
void solve(){
cin>>s; int len=s.size();
ms(nxt,-1,sizeof(nxt));
stack<int> st; int cnt=0;
FOR(i,0,len){
if (s[i]=='(') st.push(i);
if (s[i]==','){nxt[st.top()]=i; st.pop();}
if (s[i]=='?') cnt++;
}
ii ans = range(0,len-1);
cout<<cnt-ans.fi-ans.se<<'\n';
}
int main(){
//MOD=MOD1;
ios::sync_with_stdio(false);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:53:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:54:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |