Submission #52900

# Submission time Handle Problem Language Result Execution time Memory
52900 2018-06-27T06:56:49 Z Petr(#1971) Match (CEOI16_match) C++11
37 / 100
2000 ms 4108 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second

int n;
char s[100010];
char ans[100010];

int stk[1000010], top;

bool solve(int l, int r){
	if(l>r) return 1;
	if(s[r] == s[l]){
		if(solve(l+1, r-1)){
			ans[l]='('; ans[r]=')';
			return 1;
		}
	}
	int top_base = top;
	for(int i=r; l<=i; --i){
		if(top == top_base || stk[top-1] != s[i]) stk[top++] = s[i];
		else {
			--top;
			if(top == top_base){
				if(l<i-1 && s[l]==s[i-1]){
					if(solve(l+1, i-2) && solve(i, r)){
						ans[l]='('; ans[i-1]=')';
						top = top_base;
						return 1;
					}
				}
			}
		}
	}
	return 0;
}

int main()
{
	scanf("%s", s+1);
	n = strlen(s+1);
	//n = min(n, 2000);
	
	if(n&1){ puts("-1"); return 0; }
	
	if(solve(1, n)){
		puts(ans + 1);
	} else puts("-1");
	
	return 0;
}

Compilation message

match.cpp: In function 'void read(int&)':
match.cpp:6:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(int& x){ scanf("%d",&x); }
                    ~~~~~^~~~~~~~~
match.cpp: In function 'void read(ll&)':
match.cpp:7:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(ll& x){ scanf("%lld",&x); }
                   ~~~~~^~~~~~~~~~~
match.cpp: In function 'int main()':
match.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", s+1);
  ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 3 ms 408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 3 ms 408 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 520 KB Output is correct
7 Correct 3 ms 544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 3 ms 408 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 520 KB Output is correct
7 Correct 3 ms 544 KB Output is correct
8 Correct 5 ms 544 KB Output is correct
9 Correct 11 ms 800 KB Output is correct
10 Correct 6 ms 800 KB Output is correct
11 Correct 4 ms 928 KB Output is correct
12 Correct 91 ms 2424 KB Output is correct
13 Correct 43 ms 2708 KB Output is correct
14 Correct 639 ms 3456 KB Output is correct
15 Correct 6 ms 3984 KB Output is correct
16 Correct 8 ms 3984 KB Output is correct
17 Correct 87 ms 4108 KB Output is correct
18 Correct 11 ms 4108 KB Output is correct
19 Correct 1032 ms 4108 KB Output is correct
20 Correct 414 ms 4108 KB Output is correct
21 Execution timed out 2056 ms 4108 KB Time limit exceeded