답안 #127756

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
127756 2019-07-10T05:31:06 Z sealnot123 괄호 문자열 (CEOI16_match) C++14
100 / 100
19 ms 11724 KB
#include<bits/stdc++.h>
#define x first
#define y second
#define all(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define push_back pb
#define emplace_back eb
using namespace std;
typedef long long LL;
typedef double DD;
typedef long double LD;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const int N = 100007;
int dp[N][26];
char str[N], ans[N];
stack<char> sta;
int n;
void play(int l, int r){
	if(l>r) return ;
	ans[l] = '(';
	int a = dp[r][str[l]-'a'];
	ans[a] = ')';
	play(l+1, a-1); play(a+1, r);
}
int main(){
	int a,b,c,d,i,j,k,l;
	scanf("%s",str+1);
	n = strlen(str+1);
	for(i=1;i<=n;i++){
		dp[i][str[i]-'a'] = i;
		a = dp[i-1][str[i]-'a'];
		if(!a) continue;
		for(j=0;j<26;j++) dp[i][j] = max(dp[i][j], dp[a-1][j]);
	}
	// check
	for(i=1;i<=n;i++){
		if(!sta.empty() && sta.top() == str[i]) sta.pop();
		else sta.push(str[i]);
	}
	if(!sta.empty()){
		printf("-1");
		return 0;
	}
	// do ans
	play(1, n);
	printf("%s",ans+1);
	return 0;
}
/*
abbccddabbccdd
abbacabbac
accaccacca
aaaabbbb
aaabcaacba (()((())))
*/

Compilation message

match.cpp: In function 'int main()':
match.cpp:27:8: warning: unused variable 'b' [-Wunused-variable]
  int a,b,c,d,i,j,k,l;
        ^
match.cpp:27:10: warning: unused variable 'c' [-Wunused-variable]
  int a,b,c,d,i,j,k,l;
          ^
match.cpp:27:12: warning: unused variable 'd' [-Wunused-variable]
  int a,b,c,d,i,j,k,l;
            ^
match.cpp:27:18: warning: unused variable 'k' [-Wunused-variable]
  int a,b,c,d,i,j,k,l;
                  ^
match.cpp:27:20: warning: unused variable 'l' [-Wunused-variable]
  int a,b,c,d,i,j,k,l;
                    ^
match.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s",str+1);
  ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 1016 KB Output is correct
9 Correct 4 ms 1144 KB Output is correct
10 Correct 3 ms 1144 KB Output is correct
11 Correct 3 ms 1144 KB Output is correct
12 Correct 10 ms 7160 KB Output is correct
13 Correct 14 ms 7740 KB Output is correct
14 Correct 14 ms 8440 KB Output is correct
15 Correct 17 ms 9720 KB Output is correct
16 Correct 17 ms 9848 KB Output is correct
17 Correct 16 ms 10360 KB Output is correct
18 Correct 19 ms 10360 KB Output is correct
19 Correct 16 ms 11000 KB Output is correct
20 Correct 10 ms 7288 KB Output is correct
21 Correct 17 ms 11724 KB Output is correct