Submission #52907

# Submission time Handle Problem Language Result Execution time Memory
52907 2018-06-27T07:15:49 Z Petr(#1971) Match (CEOI16_match) C++11
37 / 100
2000 ms 7008 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;

map<pp,int> cache;

bool solve(int l, int r){
	if(l>r) return 1;
	pp key{l, r};
	auto it = cache.find(key);
	if(it!=cache.end()) return it->second > 0;
	if(s[r] == s[l]){
		if(solve(l+1, r-1)){
			cache[key]=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)){
						top = top_base;
						cache[key]=i-1;
						return 1;
					}
				}
			}
		}
	}
	top = top_base;
	cache[key]=-1;
	return 0;
}

void make(int l, int r){
	if(l>r) return;
	pp key{l, r};
	auto it = cache.find(key);
	ans[l] = '('; ans[it->second] = ')';
	make(l+1, it->second-1);
	make(it->second+1, r);
}

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)){
		make(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:67: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 3 ms 340 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 516 KB Output is correct
4 Correct 3 ms 584 KB Output is correct
5 Correct 2 ms 584 KB Output is correct
6 Correct 2 ms 584 KB Output is correct
7 Correct 3 ms 584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 516 KB Output is correct
4 Correct 3 ms 584 KB Output is correct
5 Correct 2 ms 584 KB Output is correct
6 Correct 2 ms 584 KB Output is correct
7 Correct 3 ms 584 KB Output is correct
8 Correct 5 ms 672 KB Output is correct
9 Correct 13 ms 1064 KB Output is correct
10 Correct 6 ms 1064 KB Output is correct
11 Correct 4 ms 1092 KB Output is correct
12 Correct 97 ms 4168 KB Output is correct
13 Correct 53 ms 4604 KB Output is correct
14 Correct 698 ms 5964 KB Output is correct
15 Correct 24 ms 6568 KB Output is correct
16 Correct 23 ms 6652 KB Output is correct
17 Correct 88 ms 7008 KB Output is correct
18 Correct 27 ms 7008 KB Output is correct
19 Correct 1069 ms 7008 KB Output is correct
20 Correct 434 ms 7008 KB Output is correct
21 Execution timed out 2037 ms 7008 KB Time limit exceeded