結果

問題 No.22 括弧の対応
ユーザー 綾地寧々綾地寧々
提出日時 2015-05-02 18:48:00
言語 PHP
(8.3.4)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 690 bytes
コンパイル時間 973 ms
コンパイル使用メモリ 18,372 KB
実行使用メモリ 18,864 KB
最終ジャッジ日時 2023-09-27 12:52:13
合計ジャッジ時間 2,155 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
18,620 KB
testcase_01 AC 16 ms
18,844 KB
testcase_02 AC 16 ms
18,840 KB
testcase_03 AC 16 ms
18,648 KB
testcase_04 AC 16 ms
18,824 KB
testcase_05 AC 16 ms
18,784 KB
testcase_06 AC 15 ms
18,724 KB
testcase_07 AC 15 ms
18,760 KB
testcase_08 AC 15 ms
18,804 KB
testcase_09 AC 16 ms
18,620 KB
testcase_10 AC 15 ms
18,740 KB
testcase_11 AC 16 ms
18,864 KB
testcase_12 AC 15 ms
18,768 KB
testcase_13 AC 16 ms
18,640 KB
testcase_14 AC 16 ms
18,840 KB
testcase_15 AC 15 ms
18,724 KB
testcase_16 AC 15 ms
18,800 KB
testcase_17 AC 15 ms
18,836 KB
testcase_18 AC 15 ms
18,856 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
list($length, $point) = explode(" ",trim(fgets(STDIN)));
$tree = 0;
$str = trim(fgets(STDIN));
if ( $str[$point-1] == '(' ) {
  $other = strlen(substr($str,0,$point-1));
  $str = substr($str,$point-1);
  for ( $index=0; $index<strlen($str); $index++ ) {
    if ( $str[$index] == '(' ) {
      ++$tree;
    }
    else {
      $tree--;
    }
    if ( $tree == 0 ) {
      echo $other + $index+1 . PHP_EOL;
      break;
    }
  }
}
else {
  $str = substr($str,0,$point);
  for ( $index=$point-1; $index>=0; $index-- ) {
    if ( $str[$index] == '(' ) {
      ++$tree;
    }
    else {
      $tree--;
    }
    if ( $tree == 0 ) {
      echo $index +1 . PHP_EOL;
      break;
    }
  }
}
0