結果

問題 No.2031 Colored Brackets
ユーザー kakel-san
提出日時 2022-08-05 23:21:31
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 1,254 bytes
コンパイル時間 3,658 ms
コンパイル使用メモリ 116,764 KB
実行使用メモリ 40,448 KB
最終ジャッジ日時 2024-09-15 20:57:40
合計ジャッジ時間 6,247 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static void Main()
    {
        var n = NN;
        var s = ReadLine();
        WriteLine(Brac(n, s));
    }
    static long Brac(int n, string s)
    {
        var dp = new int[n];
        dp[0] = 1;
        for (var i = 1; i < n; ++i)
        {
            if (s[i] == '(') dp[i] = dp[i - 1] + 1;
            else dp[i] = dp[i - 1] - 1;
        }
        var br = new long[s.Length / 2 + 1];
        br[0] = 1;
        for (var i = 0; i < s.Length; ++i)
        {
            var next = (long[]) br.Clone();
            if (s[i] == '(')
            {
                for (var j = 0; j + 1 < br.Length; ++j) next[j + 1] = (next[j + 1] + br[j]) % mod;
            }
            else
            {
                for (var j = 1; j < br.Length; ++j) next[j - 1] = (next[j - 1] + br[j]) % mod;
            }
            for (var j = dp[i] + 1; j < next.Length; ++j) next[j] = 0;
            br = next;
        }
        return br[0];
    }
    static int mod = 998_244_353;
}
0