codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1153C.md (2374B)


      1 Source: https://codeforces.com/problemset/problem/1153/C
      2 
      3 C. Serval and Parenthesis Sequence
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.
     22 
     23 In his favorite math class, the teacher taught him the following interesting definitions.
     24 
     25 A parenthesis sequence is a string, containing only characters "(" and ")".
     26 
     27 A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition.
     28 
     29 We define that $|s|$ as the length of string $s$. A strict prefix $s[1\dots l]$ $(1\leq l \lt |s|)$ of a string $s = s_1s_2\dots s_{|s|}$ is string $s_1s_2\dots s_l$. Note that the empty string and the whole string are not strict prefixes of any string by the definition.
     30 
     31 Having learned these definitions, he comes up with a new problem. He writes down a string $s$ containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in $s$ independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.
     32 
     33 After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable.
     34 
     35 Input
     36 
     37 The first line contains a single integer $|s|$ ($1\leq |s|\leq 3 \cdot 10^5$), the length of the string.
     38 
     39 The second line contains a string $s$, containing only "(", ")" and "?".
     40 
     41 Output
     42 
     43 A single line contains a string representing the answer.
     44 
     45 If there are many solutions, any of them is acceptable.
     46 
     47 If there is no answer, print a single line containing ":(" (without the quotes).
     48 
     49 Examples
     50 
     51 Input
     52 
     53 ```
     54 6
     55 (?????
     56 ```
     57 
     58 Output
     59 
     60 ```
     61 (()())
     62 ```
     63 
     64 Input
     65 
     66 ```
     67 10
     68 (???(???(?
     69 ```
     70 
     71 Output
     72 
     73 ```
     74 :(
     75 ```
     76 
     77 Note
     78 
     79 It can be proved that there is no solution for the second sample, so print ":(".