結果

問題 No.22 括弧の対応
ユーザー itok217itok217
提出日時 2017-05-05 13:57:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 31 ms / 5,000 ms
コード長 721 bytes
コンパイル時間 502 ms
コンパイル使用メモリ 62,500 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 13:15:47
合計ジャッジ時間 1,439 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 9 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 9 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 31 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 11 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:31:23: warning: ‘b’ may be used uninitialized in this function [-Wmaybe-uninitialized]
           cout << b + 1 << endl;
                       ^

ソースコード

diff #

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

int main() {
  int N, K, b;
  cin >> N >> K;
  string str;
  cin >> str;
  vector<vector<int>> a(N, vector<int>(2));
  for (int i = 0; i < N; i++) {
    if (str[i] == '(') {
      a[i][0] = 1;
    } else {
      a[i][0] = 2;
    }
  }
  while (true) {
    for (int i = 0; i < N; i++) {
      if (a[i][0] == 1 && !a[i][1]) {
        b = i;
        continue;
      }
      if (a[i][0] == 2 && !a[i][1]) {
        a[b][1] = a[i][1] = 1;
        if (b == K - 1) {
          cout << i + 1 << endl;
          return 0;
        } else if (i == K - 1) {
          cout << b + 1 << endl;
          return 0;
        }
        break;
      }
    }
  }
}
0