結果

問題 No.22 括弧の対応
コンテスト
ユーザー chiyoda
提出日時 2016-06-26 12:12:36
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 724 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 329 ms
コンパイル使用メモリ 75,600 KB
実行使用メモリ 83,572 KB
最終ジャッジ日時 2026-04-02 21:48:14
合計ジャッジ時間 965 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:38:11: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
   38 |   cout << res << endl;
      |           ^~~
main.cpp:12:7: note: 'res' was declared here
   12 |   int res;
      |       ^~~

ソースコード

diff #
raw source code

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

int main() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  int sum[10000] = {};
  int cnt = 0;
  int res;
  if (s[k -1] == '(') {
    for (int i = 0; i < n; ++i) {
    if (s[i] == '(') cnt++;
    else cnt--;
    sum[i] = cnt;
    }
    for (int i = k; i < n; ++i) {
      if (sum[k - 1] - 1 == sum[i]) {
        res = i + 1;
        break;
      }
    }
  } else {
      for (int i = k - 2; i >= 0; --i) {
    if (s[i] == ')') cnt++;
    else cnt--;
    sum[i] = cnt;
    }
    for (int i = k - 2; i >= 0; --i) {
      if (sum[k - 1] - 1 == sum[i]) {
        res = i + 1;
        break;
      }
    }
  }
  cout << res << endl;
  return 0;
}
0