Submission #666184

#TimeUsernameProblemLanguageResultExecution timeMemory
666184KahouHomework (CEOI22_homework)C++14
100 / 100
101 ms68132 KiB
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mk make_pair
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 2e6 + 50;
int n;
string s;
pii ch[N];
bool mark[N];
int cnt[N];
pii dp[N];
stack<int> st;

void input() {
	cin >> s;
	for (int i = 0; i < (int)s.size(); i++) {
		if (s[i] == 'm') {
			n++;
			st.push(n);
			mark[n] = (s[i+1] == 'a');
			i += 3;
			continue;
		}
		else if (s[i] == '?') {
			n++;
			if (st.empty()) continue;
			if (ch[st.top()].F) ch[st.top()].S = n;
			else ch[st.top()].F = n;
		}
		else if (s[i] == ')') {
			int u = st.top();
			st.pop();
			if (st.empty()) continue;
			if (ch[st.top()].F) ch[st.top()].S = u;
			else ch[st.top()].F = u;
		}
	}
}

void dfs(int u) {
	if (!ch[u].F) return;
	int v1 = ch[u].F, v2 = ch[u].S;
	dfs(v1);
	dfs(v2);
	dp[u].F = (mark[u]? dp[v1].F + dp[v2].F + 1: min(dp[v1].F, dp[v2].F));
	dp[u].S = (mark[u]? min(dp[v1].S, dp[v2].S): dp[v1].S+dp[v2].S+1);
}

void solve() {
	input();
	dfs(1);
	cout << (n+1)/2 - dp[1].F - dp[1].S << endl;
}
int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	solve();
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...