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;
const int MXN = 5e3 + 5;
int n, r, cnt;
string s;
int adj[MXN][2];
int dp[MXN][MXN];
int sz[MXN];
int t[MXN];
void dfs(int a)
{
if (t[a] == 2)
{
dp[a][1] = 1;
sz[a] = 1;
return;
}
dfs(adj[a][0]), dfs(adj[a][1]);
if (t[a] == 1)
{
for (int i = 1; i <= sz[adj[a][0]]; i++)
{
int f = 0;
for (int j = 1; j <= sz[adj[a][1]]; j++)
{
f |= dp[adj[a][1]][j];
if (dp[adj[a][0]][i] && f)
{
dp[a][i + j] = 1;
}
}
}
for (int i = 1; i <= sz[adj[a][1]]; i++)
{
int f = 0;
for (int j = 1; j <= sz[adj[a][0]]; j++)
{
f |= dp[adj[a][0]][j];
if (dp[adj[a][1]][i] && f)
{
dp[a][i + j] = 1;
}
}
}
}
else
{
for (int i = 1; i <= sz[adj[a][0]]; i++)
{
int f = 0;
for (int j = sz[adj[a][1]]; j >= 0; j--)
{
if (dp[adj[a][0]][i] && f)
{
dp[a][i + j] = 1;
}
f |= dp[adj[a][1]][j];
}
}
for (int i = 1; i <= sz[adj[a][1]]; i++)
{
int f = 0;
for (int j = sz[adj[a][0]]; j >= 0; j--)
{
if (dp[adj[a][1]][i] && f)
{
dp[a][i + j] = 1;
}
f |= dp[adj[a][0]][j];
}
}
}
sz[a] = sz[adj[a][0]] + sz[adj[a][1]];
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.length();
string nw;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == ',') continue;
if (s[i] == '?')
{
t[nw.size()] = 2;
nw.push_back('(');
nw.push_back(')');
}
else if (s[i] == '(' || s[i] == ')') nw.push_back(s[i]);
else
{
if (s[i + 1] == 'i') t[nw.size()] = 0;
else t[nw.size()] = 1;
i += 2;
}
}
vector<int> v;
vector<int> R(nw.size(), 0);
for (int i = (int)nw.size() - 1; i >= 0; i--)
{
if (nw[i] == ')') v.push_back(i);
else R[i] = v.back(), v.pop_back();
}
v.clear();
for (int i = (int)nw.size() - 1; i >= 0; i--)
{
if (nw[i] == '(')
{
vector<int> tmp;
while (!v.empty() && v.back() <= R[i]) tmp.push_back(v.back()), v.pop_back();
assert(tmp.size() == 2 || tmp.empty());
v.push_back(i);
if (tmp.size() == 2)
{
adj[i][0] = tmp[0], adj[i][1] = tmp[1];
}
}
}
assert(v.size() == 1);
dfs(v[0]);
int res = 0;
for (int i = 1; i <= n; i++)
{
res += dp[v[0]][i];
}
cout << res << '\n';
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:88:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i = 0; i < s.length(); 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... |