結果
| 問題 |
No.22 括弧の対応
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-12-17 11:16:13 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 654 bytes |
| コンパイル時間 | 3,273 ms |
| コンパイル使用メモリ | 123,136 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-16 19:56:40 |
| 合計ジャッジ時間 | 4,242 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
コンパイルメッセージ
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.
ソースコード
#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;
}