No.3143 Colorless Green Parentheses Sleep Furiously
Story
((())))()()(It is reasonable to assume that neither parenthesis sequence (1) nor (2) has ever appeared in a formula representing $1$. However, while (1) does not appear in formulas, it is correct, whereas (2) is not correct.
Problem Statement
A string obtained by concatenating one or more (any number of) copies of +1 to 1 is called a simple addition.
A string $X$ satisfying one of the following is called an expression:
- $X$ is
1 - $X$ is not
1, and $X$ becomes a simple addition after repeating the following operation as many times as possible: "Take a simple addition $Y$ contained in $X$ as a substring such that it forms($Y$), and replace that part with1."
Examples of expressions: (1+1)+(1+1), ((1+1)+1)+1
Examples of strings that are not expressions: (1), ((1+1))+1
For an expression $T$, let $f(T)$ be its value when evaluated as a mathematical formula.
For example, $f($1+(1+1)$)=3$ and $f($1+((1+1)+1)$)=4$.
Let $g(T)$ be the string obtained by deleting all characters other than ( and ) from $T$ and concatenating the remaining characters while preserving their order.
For example, $g($1+(1+1)$)=$() and $g($1+((1+1)+1)$)=$(()).
You are given positive integers $N, K$ and a string $S$ of length $N$ consisting of ( and ).
Determine whether there exists an expression $E$ satisfying $f(E)=K$ and $g(E)=S$. If one exists, output one such expression.
Input
$N\ K$ $S$
- $1\leq N\leq 10^5$
- $1\leq K\leq 10^5$
- $S$ is a string of length $N$ consisting of
(and)
Output
If there exists an expression $E$ satisfying the conditions, print Yes on the first line; otherwise, print No.
If the answer is Yes, then print one such $E$ on the second line. If the answer is No, print only one line.
Print a newline at the end.
Sample
Sample 1
Input
6 5 ()(())
Output
Yes (1+1)+(1+(1+1))
$f($(1+1)+(1+(1+1))$)=5$ and $g($(1+1)+(1+(1+1))$)=$()(()), so (1+1)+(1+(1+1)) indeed satisfies the conditions.
You may also output (1+1)+((1+1)+1).
Sample 2
Input
4 2 ()()
Output
No
No expression $E$ satisfying the conditions exists.
Sample 3
Input
8 9 ()()()()
Output
Yes (1+1)+(1+1)+(1+1)+(1+1)+1
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
nauclhlt