Created
April 14, 2019 05:12
-
-
Save whatalnk/d6fbe279fe2a8c933f194c3aad10231d to your computer and use it in GitHub Desktop.
Codeforces #551 Div2 C - Serval and Parenthesis Sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = gets.chomp.to_i | |
s = gets.chomp | |
a = 0 | |
b = 0 | |
n.times do |i| | |
if s[i] == "(" | |
a += 1 | |
elsif s[i] == ")" | |
b += 1 | |
end | |
end | |
q = n - (a + b) | |
c = a - b | |
if s[-1] == "(" || (q - c).odd? | |
puts ":(" | |
else | |
y = (q + c) / 2 | |
x = y - c | |
ans = [] | |
sum = 0 | |
n.times do |i| | |
if s[i] == "?" | |
if x != 0 | |
ans << "(" | |
x -= 1 | |
sum += 1 | |
else | |
ans << ")" | |
sum -= 1 | |
end | |
elsif s[i] == "(" | |
sum += 1 | |
ans << s[i] | |
else | |
sum -= 1 | |
ans << s[i] | |
end | |
if sum == 0 && i < n - 1 | |
puts ":(" | |
exit | |
end | |
end | |
if sum == 0 | |
puts ans.join("") | |
else | |
puts ":(" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment