Submission #110861

# Submission time Handle Problem Language Result Execution time Memory
110861 2019-05-12T16:09:41 Z mosesmayer Match (CEOI16_match) C++17
100 / 100
19 ms 11776 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int mxsz = 1e5 + 5;

int n;
char S[mxsz], ans[mxsz];
int lft[mxsz][26];

void prec(){
	for (int i = 1, p; i <= n; i++){
		p = S[i] - 'a';
		lft[i][p] = i;
		if (i - 1)
			for (int j = 0, x = lft[i-1][p]; j < 26; j++)
				if (x && (j^p))
					lft[i][j] = lft[x-1][j]; // link to before prev to be able to match prev with i
	}
	//for (int i = 1; i <= n; i++) for (int j = 0; j < 26; j++) cout << lft[i][j] << " \n"[j==25];
}
stack<int> st;
inline bool CHECK(){
	for (int i = 1, p; i <= n; i++){
		p = S[i] - 'a';
		if (st.empty() || st.top() != p) st.push(p);
		else st.pop();
	}
	return st.empty();
}
void rec(int l, int r){
	//cout << l << ' ' << r << '\n';
	if (l > r) return;
	int pos = lft[r][S[l] - 'a'];
	ans[l] = '('; ans[pos] = ')';
	rec(l+1, pos - 1); rec(pos+1, r);
}
int main(){
	scanf("%s", S+1);
	n = strlen(S+1);
	ans[n+1] = '\0';
	if (!CHECK()) return 0 * printf("-1");
	prec();
	rec(1,n);
	puts(ans+1);
	return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:39: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 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 3 ms 512 KB Output is correct
8 Correct 4 ms 1024 KB Output is correct
9 Correct 4 ms 1280 KB Output is correct
10 Correct 6 ms 1152 KB Output is correct
11 Correct 3 ms 1152 KB Output is correct
12 Correct 13 ms 7296 KB Output is correct
13 Correct 13 ms 7808 KB Output is correct
14 Correct 16 ms 8576 KB Output is correct
15 Correct 16 ms 9856 KB Output is correct
16 Correct 16 ms 9728 KB Output is correct
17 Correct 14 ms 10496 KB Output is correct
18 Correct 17 ms 10252 KB Output is correct
19 Correct 19 ms 11008 KB Output is correct
20 Correct 14 ms 7424 KB Output is correct
21 Correct 17 ms 11776 KB Output is correct