結果

問題 No.22 括弧の対応
コンテスト
ユーザー beatdjam
提出日時 2016-09-29 15:50:23
言語 PHP
(8.5.4)
コンパイル:
php -l _filename_
実行:
php _filename_
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 789 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,050 ms
コンパイル使用メモリ 38,012 KB
実行使用メモリ 76,136 KB
最終ジャッジ日時 2026-04-02 21:49:26
合計ジャッジ時間 2,320 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #
raw source code

<?php
$input_lines = trim(fgets(STDIN));
$input_lines = str_replace(array("\r\n","\r","\n"), "", $input_lines);
do {
    $tmpStr[] = $input_lines;
    $input_lines = trim(fgets(STDIN));
    $input_lines = str_replace(array("\r\n","\r","\n"), "", $input_lines);
} while ($input_lines !== "");

$tmp = $tmpStr[1];
$tmpPos = explode(" ",$tmpStr[0])[1];
$s = str_split($tmp);

$tmpCnt = 0;
if($s[$tmpPos - 1] === "("){
	$max = count($s);
	
	for($i = $tmpPos - 1;$i < $max ;$i++){
		$tmpCnt += getCountVal($s[$i]);
		
		if($tmpCnt === 0){
			break;
		}
	}
}else{
	for($i = $tmpPos - 1;$i >= 0 ;$i--){
		$tmpCnt += getCountVal($s[$i]);
		
		if($tmpCnt === 0){
			break;
		}
	}
}

print $i + 1;

function getCountVal($i_string){
		if($i_string === "("){
			return 1;
		}else{
			return -1;
		}
}
0