Last active
June 7, 2021 04:07
-
-
Save sakapon/14ea0409ab21124de6af17157d0f2eac to your computer and use it in GitHub Desktop.
競プロ典型 90 問 / Q002
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
using System; | |
class Q002 | |
{ | |
static void Main() | |
{ | |
var n = int.Parse(Console.ReadLine()); | |
for (int x = 0; x < 1 << n; x++) | |
{ | |
var b = ToBracketSeq(x, n); | |
if (IsRegularBracketSeq(b)) Console.WriteLine(b); | |
} | |
} | |
static string ToBracketSeq(int x, int n) | |
{ | |
var s = new char[n]; | |
for (int i = 0; i < n; i++) | |
s[n - 1 - i] = (x & (1 << i)) == 0 ? '(' : ')'; | |
return new string(s); | |
} | |
static bool IsRegularBracketSeq(string s) | |
{ | |
var t = 0; | |
foreach (var c in s) | |
if (c == '(') ++t; | |
else if (c == ')') if (--t < 0) return false; | |
return t == 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://atcoder.jp/contests/typical90/submissions/23268934