답안 #52815

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52815 2018-06-27T00:56:07 Z ainta(#1378) 괄호 문자열 (CEOI16_match) C++11
37 / 100
2000 ms 852 KB
#include<cstdio>
#include<algorithm>
#define N_ 101000
using namespace std;
char p[N_], st[N_], r[N_], st2[N_];
int n;
bool Pos(int pv) {
	int i, top = 0;
	for (i = 1; i <= pv; i++) {
		if (r[i] == '(')st[++top] = p[i];
		else {
			if (top && st[top] == p[i])top--;
			else return false;
		}
	}
	int pt = 1, ed = n;
	int top2 = 0;
	for (i = n; i > pv; i--) {
		if (top2 && st2[top2] == p[i]) top2--;
		else {
			if (!top2 && pt <= top && st[pt] == p[i])pt++;
			else {
				st2[++top2] = p[i];
			}
		}
	}
	return pt == top + 1 && top2 == 0;
}
int main() {
	//freopen("input.txt", "r", stdin);
	int i;
	scanf("%s", p + 1);
	for (i = 1; p[i]; i++);
	n = i - 1;
	if (!Pos(0)) {
		puts("-1");
		return 0;
	}
	for (i = 1; i <= n; i++) {
		r[i] = '(';
		if (!Pos(i)) {
			r[i] = ')';
		}
	}
	printf("%s\n", r + 1);
}

Compilation message

match.cpp: In function 'bool Pos(int)':
match.cpp:16:14: warning: unused variable 'ed' [-Wunused-variable]
  int pt = 1, ed = n;
              ^~
match.cpp: In function 'int main()':
match.cpp:32:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", p + 1);
  ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 4 ms 488 KB Output is correct
5 Correct 4 ms 492 KB Output is correct
6 Correct 7 ms 516 KB Output is correct
7 Correct 11 ms 560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 4 ms 488 KB Output is correct
5 Correct 4 ms 492 KB Output is correct
6 Correct 7 ms 516 KB Output is correct
7 Correct 11 ms 560 KB Output is correct
8 Correct 175 ms 688 KB Output is correct
9 Correct 172 ms 688 KB Output is correct
10 Correct 140 ms 688 KB Output is correct
11 Correct 114 ms 752 KB Output is correct
12 Execution timed out 2056 ms 852 KB Time limit exceeded
13 Halted 0 ms 0 KB -