Submission #52911

# Submission time Handle Problem Language Result Execution time Memory
52911 2018-06-27T07:17:18 Z Petr(#1971) Match (CEOI16_match) C++11
100 / 100
1758 ms 7220 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;
		} else {
			cache[key]=-1;
			return 0;
		}
	}
	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;
					} else {
						top = top_base;
						cache[key]=-1;
						return 0;
					}
				}
			}
		}
	}
	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:74: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 376 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 636 KB Output is correct
7 Correct 3 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 3 ms 636 KB Output is correct
7 Correct 3 ms 636 KB Output is correct
8 Correct 4 ms 772 KB Output is correct
9 Correct 9 ms 924 KB Output is correct
10 Correct 7 ms 1004 KB Output is correct
11 Correct 4 ms 1104 KB Output is correct
12 Correct 69 ms 4176 KB Output is correct
13 Correct 58 ms 4452 KB Output is correct
14 Correct 503 ms 5728 KB Output is correct
15 Correct 29 ms 6288 KB Output is correct
16 Correct 25 ms 6324 KB Output is correct
17 Correct 83 ms 6688 KB Output is correct
18 Correct 28 ms 6688 KB Output is correct
19 Correct 916 ms 6688 KB Output is correct
20 Correct 365 ms 6688 KB Output is correct
21 Correct 1758 ms 7220 KB Output is correct