結果

問題 No.22 括弧の対応
ユーザー te-sh
提出日時 2017-01-11 12:22:04
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 496 bytes
コンパイル時間 825 ms
コンパイル使用メモリ 106,624 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 06:17:58
合計ジャッジ時間 1,538 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  size_t[] rd = readln.split.to!(size_t[]);
  size_t n = rd[0], k = --rd[1];
  string s = readln.chomp;

  size_t r = s[k].predSwitch('(', search(s[k..$]) + k,
                             ')', k - search(s[0..k+1].retro));
  writeln(++r);
}

size_t search(InputRange)(InputRange s)
{
  int d = 0;
  foreach (i, c; s.enumerate) {
    d += c.predSwitch('(', +1, ')', -1);
    if (d == 0) return i;
  }
  return 0;
}
0