Skip to content

Instantly share code, notes, and snippets.

@tokugh
Last active June 4, 2021 15:26
Show Gist options
  • Save tokugh/44e4f7f5506c8b8a108df4a75aaafd86 to your computer and use it in GitHub Desktop.
Save tokugh/44e4f7f5506c8b8a108df4a75aaafd86 to your computer and use it in GitHub Desktop.
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