QOJ.ac

QOJ

时间限制: 0.5 s 内存限制: 1024 MB 总分: 100 可 Hack ✓

#18697. Ternary Operator

统计

You are given an expression containing $N$ boolean variables. Write a program to calculate the number of ways to assign values to these variables such that the value of the expression is $0$, out of the $2^N$ possible assignments.

In this problem, a valid expression is defined by the following BNF notation:

  • boolean ::= '0' | '1'
  • variable ::= 'a' | 'b' | ... | $N$-th lowercase alphabet letter
  • value ::= boolean | variable
  • condition ::= value '==' value
  • expression ::= value | condition '?' expression ':' expression

The value of an expression is denoted by $eval(expression)$ and is calculated recursively as follows. It can be observed that for any given valid expression, there is a unique way to evaluate it.

  • $eval(value) = value$
  • $eval(condition) = eval(value_1 \text{ '==' } value_2) = \begin{cases} 1 & \text{if } eval(value_1) = eval(value_2) \\ 0 & \text{otherwise} \end{cases}$
  • $eval(expression) = eval(value) = value$
  • $eval(expression) = eval(condition \text{ '?' } expression_1 \text{ ':' } expression_2) = \begin{cases} eval(expression_1) & \text{if } eval(condition) = 1 \\ eval(expression_2) & \text{otherwise} \end{cases}$

Input

The first line contains the number of variables $N$ ($1 \le N \le 26$). The second line contains a string representing the expression, with a length between $1$ and $1\,000$ inclusive. The expression consists only of '0', '1', 'a' through the $N$-th lowercase alphabet letter, '=', '?', and ':', and is guaranteed to be a valid expression.

Output

Output the number of ways to assign values to the variables such that the value of the expression is $0$.

Examples

Input 1

2
a==b?a:0

Output 1

3

Input 2

10
0

Output 2

1024

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.