結果

問題 No.22 括弧の対応
ユーザー augusuto04
提出日時 2015-06-07 17:47:56
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 563 bytes
コンパイル時間 516 ms
コンパイル使用メモリ 57,436 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 07:02:20
合計ジャッジ時間 1,028 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
  int n, k, count = 1;
  cin >> n >> k;
  string str;
  cin >> str;
  k--;
  if(str[k] == '('){
    for(int i = k + 1; i < n; i++){
      if(str[i] == '(')
        count++;
      else
        count--;
      if(count == 0){
        cout << i + 1 << endl;
        break;
      }
    }
  } else {
    for(int i = k - 1; i >= 0; i--){
      if(str[i] == ')')
        count++;
      else
        count--;
      if(count == 0){
        cout << i + 1 << endl;
        break;
      }
    }
  }
}
0