답안 #52825

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52825 2018-06-27T03:39:27 Z tataky(#1371) 괄호 문자열 (CEOI16_match) C++11
10 / 100
6 ms 492 KB
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

int a[18], n;
char s[100001];
vector<int> v;

bool ok() {
	int c = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] == 0) c++;
		else c--;
		if (c < 0) return false;
	}
	if (c) return false;
	v.clear();
	for (int i = 0; i < n; i++) {
		if (a[i] == 0) v.push_back(i);
		else {
			if (s[v.back()] != s[i]) return false;
			v.pop_back();
		}
	}
	return true;
}

int main() {
	scanf("%s", s);
	n = strlen(s);
	if (n > 18) {
		puts("no human");
		return 0;
	}
	for (int i = 0; i < (1 << n); i++) {
		for (int j = 0; j < n; j++) {
			if (i&(1 << (n - 1 - j))) a[j] = 1;
			else a[j] = 0;
		}
		if (ok()) {
			for (int j = 0; j < n; j++) {
				if (i&(1 << (n - 1 - j))) printf(")");
				else printf("(");
			}
			puts("");
			return 0;
		}
	}
	puts("-1");
	return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:31: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 2 ms 256 KB Output is correct
2 Correct 2 ms 360 KB Output is correct
3 Correct 6 ms 416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 360 KB Output is correct
3 Correct 6 ms 416 KB Output is correct
4 Incorrect 2 ms 492 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 360 KB Output is correct
3 Correct 6 ms 416 KB Output is correct
4 Incorrect 2 ms 492 KB Output isn't correct
5 Halted 0 ms 0 KB -