Submission #53515

# Submission time Handle Problem Language Result Execution time Memory
53515 2018-06-30T06:50:23 Z baactree Match (CEOI16_match) C++17
10 / 100
5 ms 3620 KB
#include <bits/stdc++.h>
using namespace std;
int n;
char str[100005];
vector<int> p[256][256];
int s[100005];
void solve(int le, int ri) {
	if (le > ri)return;
	auto it = upper_bound(p[s[le - 1]][str[le]].begin(), p[s[le - 1]][str[le]].end(), ri);
	int i = *(--it);
	str[le] = '(';
	str[i] = ')';
	solve(le + 1, i - 1);
	solve(i + 1, ri);
	return;
}
int main() {
	scanf("%s", str + 1);
	n = strlen(str + 1);
	stack<int> st;
	for (int i = 1; i <= n; i++) {
		if (!st.empty() && st.top() == str[i])st.pop();
		else st.push(str[i]);
	}
	if (!st.empty())return !printf("-1\n");
	st.push(0);
	p[0][0].push_back(0);
	s[0] = 0;
	for (int i = 1; i <= n; i++) {
		if (st.top() == str[i])st.pop();
		else st.push(str[i]);
		p[st.top()][str[i]].push_back(i);
		s[i] = st.top();
	}
	solve(1, n);
	printf("%s\n", str + 1);
	return 0;
}

Compilation message

match.cpp: In function 'void solve(int, int)':
match.cpp:9:44: warning: array subscript has type 'char' [-Wchar-subscripts]
  auto it = upper_bound(p[s[le - 1]][str[le]].begin(), p[s[le - 1]][str[le]].end(), ri);
                                            ^
match.cpp:9:75: warning: array subscript has type 'char' [-Wchar-subscripts]
  auto it = upper_bound(p[s[le - 1]][str[le]].begin(), p[s[le - 1]][str[le]].end(), ri);
                                                                           ^
match.cpp: In function 'int main()':
match.cpp:32:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   p[st.top()][str[i]].push_back(i);
                     ^
match.cpp:18:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", str + 1);
  ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 2004 KB Output is correct
3 Correct 3 ms 2004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 2004 KB Output is correct
3 Correct 3 ms 2004 KB Output is correct
4 Runtime error 5 ms 3620 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 2004 KB Output is correct
3 Correct 3 ms 2004 KB Output is correct
4 Runtime error 5 ms 3620 KB Execution killed with signal 11 (could be triggered by violating memory limits)
5 Halted 0 ms 0 KB -