Last active
June 4, 2021 15:26
-
-
Save tokugh/44e4f7f5506c8b8a108df4a75aaafd86 to your computer and use it in GitHub Desktop.
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
use proconio::{fastout, input}; | |
#[fastout] | |
fn main() { | |
input! { n: usize, }; | |
for tmp in 0..(1 << n) { // 上位ビットが文字列の左側に相当 | |
let mut cnt = 0; | |
let mut ok = true; | |
for i in (0..n).rev() { // 上位ビットから順に走査 | |
if (tmp >> i) & 1 == 0 { | |
cnt += 1; | |
} else { | |
cnt -= 1; | |
ok &= cnt >= 0; | |
} | |
} | |
ok &= cnt == 0; | |
if ok { | |
for i in (0..n).rev() { // 上位ビットから順に走査 | |
print!("{}", if (tmp >> i) & 1 == 0 { '(' } else { ')' }) | |
} | |
println!(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment