#include <bits/stdc++.h>
// #define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<pii,int> ipii;
typedef pair<pii,pii> ipiii;
const int MAXN = 6e6+10;
const int SQRT = 300;
const int MAXA = 50;
const int LOG = 20;
const int INF = 1e18+10;
const ld EPS = 1e-6;
const int MOD = 998244353;
int sum(int a, int b){ return (a+b)%MOD; }
void chsum(int &a, int b){ a = (a+b)%MOD; }
void chsub(int &a, int b){ a = (a+MOD-b)%MOD; }
int mul(int a, int b){ return (a*b)%MOD; }
void chmul(int &a, int b){ a = (a*b)%MOD; }
void chmn(int &a, int b){ a = min(a, b); }
void chmx(int &a, int b){ a = max(a, b); }
int n, id, ty[MAXN], le[MAXN], ri[MAXN];
vector <int> adj[MAXN];
string s;
struct node {
int le, ri;
} A[MAXN];
void solve(int l, int r){
if(s[l+1]=='a') ty[++id] = 1;
else ty[++id] = 2;
int nw = id, en;
if(s[r-1]=='?'){
adj[nw].pb(++id); en = r-3;
} else {
adj[nw].pb(id+1);
solve(le[r-1]-3, r-1); en = le[r-1]-3-2;
}
if(s[l+4]=='?'){
// assert(l+4==en);
adj[nw].pb(++id);
} else {
adj[nw].pb(id+1);
solve(l+4, en);
}
// cout << l << ' '<< r << ' ' <<id<<" lr\n";
// for(auto nx : adj[nw]) cout << nx << "nx\n";
}
void dfs(int nw){
A[nw].le = A[nw].ri = 0;
if(ty[nw]==0) return;
vector <node> vec;
for(auto nx : adj[nw]){
dfs(nx);
vec.pb(A[nx]);
}
if(ty[nw]==1)
A[nw].le = min(vec[0].le, vec[1].le),
A[nw].ri = vec[0].ri+vec[1].ri+1;
else
A[nw].le = vec[0].le+vec[1].le+1,
A[nw].ri = min(vec[0].ri, vec[1].ri);
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>s;
vector <int> vec;
for(int i=0; i<s.size(); i++){
if(s[i]=='?') n++;
if(s[i]=='(') vec.pb(i);
else if(s[i]==')'){
int ba = vec.back();
ri[ba] = i; le[i] = ba;
vec.pop_back();
}
}
solve(0, (int)s.size()-1);
dfs(1);
cout << n-A[1].ri-A[1].le << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp:20:21: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
20 | const int INF = 1e18+10;
| ~~~~^~~
# | 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... |