結果
| 問題 |
No.22 括弧の対応
|
| コンテスト | |
| ユーザー |
f843nmfwisfeuw
|
| 提出日時 | 2020-08-24 20:17:15 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 1,103 bytes |
| コンパイル時間 | 949 ms |
| コンパイル使用メモリ | 97,500 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-20 07:52:10 |
| 合計ジャッジ時間 | 1,655 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <iterator>
#include <set>
#include <queue>
#include <bitset>
#include <cassert>
using namespace std;
// S[K-1]が(か)によってかわるのでは
// ( をStackにいれておく, S[K-1]の(がPopした時に出たら、それと対応する)が答え
// )がS[K-1]なら、その時にPopしたStackのが対応する
// Stackには(の位置をいれる
// つねに(がSのiの位置にある時に対応する)はj(i+1<=j)の位置にある
int main() {
int N, K;
cin >> N >> K;
string S;
cin >> S;
stack<int> s;
for (int i = 0; i < S.size(); ++i) {
if (S[i] == '(') {
s.push(i);
} else {
int c = s.top();
s.pop();
if (i == K - 1) {
cout << c + 1 << endl;
break;
} else if (c == K - 1) {
cout << i + 1 << endl;
break;
}
}
}
}
f843nmfwisfeuw