답안 #52880

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52880 2018-06-27T06:09:57 Z tourist(#1970) 괄호 문자열 (CEOI16_match) C++11
100 / 100
14 ms 11856 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;

int n, dt[N][26];
char a[N], b[N];

void solve (int S, int E) {
	if(S>E) return;
	int T = dt[E][a[S]];
	if(T <= S) {
		puts("-1");
		exit(0);
	}
	b[S] = '(';
	b[T] = ')';
	solve(S+1, T-1);
	solve(T+1, E);
}

int main()
{
	scanf("%s",a+1);
	n = strlen(a+1);
	for(int i=1;i<=n;i++) {
		a[i] -= 'a';
		if(i == 1 || dt[i-1][a[i]] < 2) {
			for(int j=0;j<26;j++) {
				dt[i][j] = -1;
			}
		}
		else {
			int T = dt[i-1][a[i]] - 1;
			for(int j=0;j<26;j++) {
				dt[i][j] = dt[T][j];
			}
		}
		dt[i][a[i]] = i;
	}
	solve(1, n);
	printf("%s",b+1);
}

Compilation message

match.cpp: In function 'void solve(int, int)':
match.cpp:10:20: warning: array subscript has type 'char' [-Wchar-subscripts]
  int T = dt[E][a[S]];
                    ^
match.cpp: In function 'int main()':
match.cpp:27:28: warning: array subscript has type 'char' [-Wchar-subscripts]
   if(i == 1 || dt[i-1][a[i]] < 2) {
                            ^
match.cpp:33:24: warning: array subscript has type 'char' [-Wchar-subscripts]
    int T = dt[i-1][a[i]] - 1;
                        ^
match.cpp:38:13: warning: array subscript has type 'char' [-Wchar-subscripts]
   dt[i][a[i]] = i;
             ^
match.cpp:23:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s",a+1);
  ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 3 ms 360 KB Output is correct
3 Correct 2 ms 540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 3 ms 360 KB Output is correct
3 Correct 2 ms 540 KB Output is correct
4 Correct 2 ms 564 KB Output is correct
5 Correct 2 ms 612 KB Output is correct
6 Correct 2 ms 800 KB Output is correct
7 Correct 3 ms 800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 3 ms 360 KB Output is correct
3 Correct 2 ms 540 KB Output is correct
4 Correct 2 ms 564 KB Output is correct
5 Correct 2 ms 612 KB Output is correct
6 Correct 2 ms 800 KB Output is correct
7 Correct 3 ms 800 KB Output is correct
8 Correct 3 ms 1260 KB Output is correct
9 Correct 3 ms 1388 KB Output is correct
10 Correct 3 ms 1404 KB Output is correct
11 Correct 3 ms 1420 KB Output is correct
12 Correct 8 ms 7308 KB Output is correct
13 Correct 9 ms 7964 KB Output is correct
14 Correct 13 ms 8620 KB Output is correct
15 Correct 11 ms 10028 KB Output is correct
16 Correct 11 ms 10028 KB Output is correct
17 Correct 12 ms 10556 KB Output is correct
18 Correct 13 ms 10556 KB Output is correct
19 Correct 13 ms 11196 KB Output is correct
20 Correct 8 ms 11196 KB Output is correct
21 Correct 14 ms 11856 KB Output is correct