結果

問題 No.22 括弧の対応
ユーザー prog470
提出日時 2016-09-07 13:43:05
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 798 bytes
コンパイル時間 1,137 ms
コンパイル使用メモリ 80,640 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 07:15:44
合計ジャッジ時間 1,568 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:48:17: warning: ‘ans’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   48 |         cout << ans << endl;
      |                 ^~~

ソースコード

diff #

#include<stdio.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

int main() {

	int N, K, ans;
	string S;
	cin >> N >> K >> S;

	stack<pair<int, int>> q;
	set<pair<int, int>> st;

	for (int i = 0; i < S.size(); i++) {
		if (S[i] == '(') {
			q.push(pair<int, int>(i+1, -1));
		}
		else {
			q.top().second = i + 1;
			st.insert(q.top());
			q.pop();
		}
	}

	set<pair<int, int>>::iterator it = st.begin();
	while (it != st.end()) {
		if (S[K - 1] == '(' && (*it).first == K) {
			ans = (*it).second;
			break;
		}
		else if(S[K - 1] == ')' && (*it).second == K){
			ans = (*it).first;
			break;
		}
		it++;
	}

	cout << ans << endl;

	return 0;
}
0