結果

問題 No.22 括弧の対応
ユーザー sawfishsawfish
提出日時 2022-12-17 11:16:13
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 654 bytes
コンパイル時間 3,417 ms
コンパイル使用メモリ 122,880 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 06:43:20
合計ジャッジ時間 4,325 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:16:21: warning: variable 'ans' is used uninitialized whenever 'for' loop exits because its condition is false [-Wsometimes-uninitialized]
   16 |     for (int i = 1; i <= N; i++) {
      |                     ^~~~~~
main.cpp:34:13: note: uninitialized use occurs here
   34 |     cout << ans << endl;
      |             ^~~
main.cpp:16:21: note: remove the condition if it is always true
   16 |     for (int i = 1; i <= N; i++) {
      |                     ^~~~~~
main.cpp:15:12: note: initialize the variable 'ans' to silence this warning
   15 |     int ans;
      |            ^
      |             = 0
1 warning generated.

ソースコード

diff #

#include <iostream>
#include <stack>

using namespace std;
using LL = long long;

int main() {
    int N, K;
    string S;
    cin >> N >> K >> S;

    stack<char> stk1;
    stack<int> stk2;

    int ans;
    for (int i = 1; i <= N; i++) {
        if (S[i - 1] == '(') {
            stk1.push(S[i - 1]);
            stk2.push(i);
        } else {
            if (stk2.top() == K) {
                ans = i;
                break;
            } else if (i == K) {
                ans = stk2.top();
                break;
            } else {
                stk1.pop();
                stk2.pop();
            }
        }
    }

    cout << ans << endl;
}
0