답안 #61215

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
61215 2018-07-25T11:29:41 Z junodeveloper 괄호 문자열 (CEOI16_match) C++17
100 / 100
25 ms 12852 KB
#include <bits/stdc++.h>
using namespace std;
char s[100010],a[100010];
int pre[100010][26];
void solve(int l, int r) {
	if(l > r) return;
	if(pre[r][s[l]-'a'] <= l) { puts("-1"); exit(0); }
	a[l] = '(', a[pre[r][s[l]-'a']] = ')';
	solve(l+1, pre[r][s[l]-'a']-1), solve(pre[r][s[l]-'a']+1, r);
}
int main() {
	scanf("%s", s);
	int n = strlen(s);
	for(int i=0; i<n; i++) {
		for(int j=0; j<26; j++)
			if(j == s[i]-'a') pre[i][j] = i;
			else if(i && pre[i-1][s[i]-'a'] > 0) pre[i][j] = pre[pre[i-1][s[i]-'a']-1][j];
			else pre[i][j] = -1;
	}
	solve(0,n-1);
	puts(a);
	return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", s);
  ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 540 KB Output is correct
4 Correct 3 ms 588 KB Output is correct
5 Correct 3 ms 588 KB Output is correct
6 Correct 4 ms 768 KB Output is correct
7 Correct 4 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 540 KB Output is correct
4 Correct 3 ms 588 KB Output is correct
5 Correct 3 ms 588 KB Output is correct
6 Correct 4 ms 768 KB Output is correct
7 Correct 4 ms 768 KB Output is correct
8 Correct 4 ms 1264 KB Output is correct
9 Correct 4 ms 1416 KB Output is correct
10 Correct 4 ms 1440 KB Output is correct
11 Correct 5 ms 1688 KB Output is correct
12 Correct 15 ms 7708 KB Output is correct
13 Correct 13 ms 8280 KB Output is correct
14 Correct 17 ms 8980 KB Output is correct
15 Correct 15 ms 10352 KB Output is correct
16 Correct 25 ms 10448 KB Output is correct
17 Correct 23 ms 11168 KB Output is correct
18 Correct 22 ms 11244 KB Output is correct
19 Correct 25 ms 11972 KB Output is correct
20 Correct 16 ms 11972 KB Output is correct
21 Correct 25 ms 12852 KB Output is correct