Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active June 7, 2021 04:07
Show Gist options
  • Save sakapon/14ea0409ab21124de6af17157d0f2eac to your computer and use it in GitHub Desktop.
Save sakapon/14ea0409ab21124de6af17157d0f2eac to your computer and use it in GitHub Desktop.
競プロ典型 90 問 / Q002
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;
}
}
@sakapon
Copy link
Author

sakapon commented Jun 2, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment