답안 #38287

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
38287 2018-01-03T13:44:38 Z UncleGrandpa925 괄호 문자열 (CEOI16_match) C++14
10 / 100
0 ms 2956 KB
/*input
aaabba
abbabb
abab 
abbaaa
*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define int long long
#define N
#define bit(x,y) ((x>>y)&1LL)
#define na(x) (#x) << ":" << x
ostream& operator << (ostream &os, vector<int>&x) {
	for (int i = 0; i < x.size(); i++) os << x[i] << sp;
	return os;
}
ostream& operator << (ostream &os, pair<int, int> x) {
	cout << x.fi << sp << x.se << sp;
	return os;
}
ostream& operator << (ostream &os, vector<pair<int, int> >&x) {
	for (int i = 0; i < x.size(); i++) os << x[i] << endl;
	return os;
}

string s;
vector<vector<int> > a(27);
int n;
int order[100005];

void solve(int l, int r) {
	if (l > r) return;
	if (l == r) {
		cout << "-1" << endl;
		exit(0);
	}
	auto it = upper_bound(a[s[l] - 'a'].begin(), a[s[l] - 'a'].end(), r) - 1;
	if (*it == l) {
		cout << "-1" << endl;
		exit(0);
	}
	order[l] = 1; order[*it] = -1;
	solve(l + 1, *it - 1); solve(*it + 1, r);
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> s; n = s.size();
	for (int i = 0; i < n; i++) {
		a[s[i] - 'a'].push_back(i);
	}
	solve(0, n - 1);
		for (int i = 0; i < n; i++) if (order[i] == 1) cout << '('; else cout << ')';
}

Compilation message

match.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<long long int>&)':
match.cpp:19:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < x.size(); i++) os << x[i] << sp;
                    ^
match.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<std::pair<long long int, long long int> >&)':
match.cpp:27:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < x.size(); i++) os << x[i] << endl;
                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2956 KB Output is correct
2 Correct 0 ms 2956 KB Output is correct
3 Correct 0 ms 2956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2956 KB Output is correct
2 Correct 0 ms 2956 KB Output is correct
3 Correct 0 ms 2956 KB Output is correct
4 Incorrect 0 ms 2956 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2956 KB Output is correct
2 Correct 0 ms 2956 KB Output is correct
3 Correct 0 ms 2956 KB Output is correct
4 Incorrect 0 ms 2956 KB Output isn't correct
5 Halted 0 ms 0 KB -