#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
#define left BAO
#define right ANH
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y)
{
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y)
{
x = y;
return true;
}
return false;
}
const int MOD = 1e9 + 7; //998244353
template<class X, class Y>
void add(X &x, const Y &y) {
x = (x + y);
if(x >= MOD) x -= MOD;
}
template<class X, class Y>
void sub(X &x, const Y &y) {
x = (x - y);
if(x < 0) x += MOD;
}
/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
const ll INF = 1e9;
const int N = 5e5 + 10;
string s;
int l[N], r[N], child[N][2], sz[N], ma[N], mi[N], t[N];
int cntNode = 0;
int buildTree(int pos) {
++cntNode;
int curr = cntNode;
if(s[pos] == '?') {
l[cntNode] = r[cntNode] = pos;
sz[cntNode] = 1;
return cntNode;
}
if(s[pos + 1] == 'a') t[curr] = 1;
int left = buildTree(pos + 4);
int right = buildTree(r[left] + 2);
child[curr][0] = left;
child[curr][1] = right;
sz[curr] = sz[left] + sz[right];
l[curr] = pos;
r[curr] = r[right] + 1;
return curr;
}
void dfs(int u) {
if(sz[u] == 1) {
mi[u] = ma[u] = 1;
return;
}
dfs(child[u][0]);
dfs(child[u][1]);
int left = child[u][0];
int right = child[u][1];
if(t[u]) {
//max(a, b)
int p = min(sz[left] - ma[left], sz[right] - ma[right]);
ma[u] = sz[u] - p;
mi[u] = mi[left] + mi[right];
} else {
//min(a, b)
mi[u] = min(mi[left], mi[right]);
int p = sz[left] - ma[left] + 1 + sz[right] - ma[right] + 1;
ma[u] = sz[u] - p + 1;
}
}
void BaoJiaoPisu() {
cin >> s;
int len = s.size(), n = 0;
for(int i = 0; i < len; i++) {
if(s[i] == '?') ++n;
}
int root = buildTree(0);
dfs(root);
cout << ma[root] - mi[root] + 1;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
//online
#endif
int tc = 1, ddd = 0;
// cin >> tc;
while(tc--) {
//ddd++;
//cout << "Case #" << ddd << ": ";
BaoJiaoPisu();
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:141:17: warning: unused variable 'ddd' [-Wunused-variable]
141 | int tc = 1, ddd = 0;
| ^~~
Main.cpp:135:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:136:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
1492 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
1492 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
1364 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
1492 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
1492 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |