๐Ÿ“„ ้›ฃๆ˜“ๅบฆ : โญโญโญ ๐Ÿ“„ ่จ€่ชž : C#

๐Ÿ“‚ๅ•้กŒ


๐Ÿ”ถ ่งฃใๆ–น

โœ” Bit Shiftใ‚’ๅˆฉ็”จใ™ใ‚‹

using System;
using System.Text;

namespace atCoder.Contest
{
    class Q_002_Encyclopedia_of_Parentheses
    {

        static void Main()
        {
            int N = Convert.ToInt32(Console.ReadLine());
            if (N % 2 == 1) return;

            // (1<<N)ๆผ”็ฎ—  == 2^N
            for (int i = 0; i < (1 << N); i++)
            {
                StringBuilder brackets = new StringBuilder();
                for (int j = N - 1; j > -1; j--)
                {
                    // i &(1<<j) ใฏใ€€iใฎj็•ช็›ฎใฎใƒ“ใƒƒใƒˆใ‚’็ขบ่ชใ™ใ‚‹ใ“ใจ
                    if ((i & (1 << j)) == 0)
                        brackets.Append('(');
                    else
                        brackets.Append(')');
                }

                int count = 0;
                for (int k = 0; k < brackets.Length; k++)
                {
                    count += brackets[k] == '(' ? 1 : -1;
                    if (count < 0) break ;
                }

                if(count == 0)
                    Console.WriteLine(brackets);
            }
        }
    }
}

(post-code: categories_sub_01)

TOP