# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
84089 |
2018-11-12T20:14:25 Z |
radoslav11 |
Match (CEOI16_match) |
C++14 |
|
111 ms |
107256 KB |
/*
We will solve the problem recursively. It's intuitive that we want to match the bracket at position 0 to the rightmost possible position. Lets have T = S[p + 1; N - 1].
If both S and T have at least one possible solving pattern then S[1; p - 1] also has a solving pattern and we can match 0 with p. So we will find the largest such p with S[p] == S[0].
Then we will solve the problem recursively on S[1, p - 1] and S[p + 1, N - 1]. If we can find p fast, the complexity of the recursion is O(N).
*/
#include <bits/stdc++.h>
#define endl '\n'
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 19);
const int SIGMA = 26;
string s;
void read()
{
cin >> s;
}
char answer[MAXN];
int last[MAXN][SIGMA];
void rec(int l, int r)
{
if(r < l)
return;
int mid = last[r][s[l] - 'a'];
if(mid == -1)
{
while(true) { }
cout << -1 << endl;
exit(0);
}
answer[l] = '(';
answer[mid] = ')';
rec(l + 1, mid - 1);
rec(mid + 1, r);
}
void solve()
{
stack<int> st;
for(int i = 0; i < SZ(s); i++)
if(!st.empty() && s[st.top()] == s[i]) st.pop();
else st.push(i);
if(!st.empty())
{
cout << -1 << endl;
return;
}
memset(last, -1, sizeof(last));
for(int i = 0; i < SZ(s); i++)
{
last[i][s[i] - 'a'] = i;
if(i >= 1)
{
int pos = last[i - 1][s[i] - 'a'];
for(int c = 0; c < SIGMA; c++)
if(pos && c != (s[i] - 'a'))
last[i][c] = last[pos - 1][c];
}
}
rec(0, SZ(s) - 1);
for(int i = 0; i < SZ(s); i++)
cout << answer[i];
cout << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
53628 KB |
Output is correct |
2 |
Correct |
45 ms |
53772 KB |
Output is correct |
3 |
Correct |
2 ms |
53772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
53628 KB |
Output is correct |
2 |
Correct |
45 ms |
53772 KB |
Output is correct |
3 |
Correct |
2 ms |
53772 KB |
Output is correct |
4 |
Runtime error |
111 ms |
107256 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 |
46 ms |
53628 KB |
Output is correct |
2 |
Correct |
45 ms |
53772 KB |
Output is correct |
3 |
Correct |
2 ms |
53772 KB |
Output is correct |
4 |
Runtime error |
111 ms |
107256 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |