# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
84101 |
2018-11-12T21:08:04 Z |
radoslav11 |
Match (CEOI16_match) |
C++14 |
|
25 ms |
12940 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 = (int)1e5 + 42;
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'];
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 > 0 && 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 |
10 ms |
10488 KB |
Output is correct |
2 |
Correct |
10 ms |
10612 KB |
Output is correct |
3 |
Correct |
2 ms |
10612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
10488 KB |
Output is correct |
2 |
Correct |
10 ms |
10612 KB |
Output is correct |
3 |
Correct |
2 ms |
10612 KB |
Output is correct |
4 |
Correct |
10 ms |
10612 KB |
Output is correct |
5 |
Correct |
10 ms |
10612 KB |
Output is correct |
6 |
Correct |
10 ms |
10644 KB |
Output is correct |
7 |
Correct |
10 ms |
10776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
10488 KB |
Output is correct |
2 |
Correct |
10 ms |
10612 KB |
Output is correct |
3 |
Correct |
2 ms |
10612 KB |
Output is correct |
4 |
Correct |
10 ms |
10612 KB |
Output is correct |
5 |
Correct |
10 ms |
10612 KB |
Output is correct |
6 |
Correct |
10 ms |
10644 KB |
Output is correct |
7 |
Correct |
10 ms |
10776 KB |
Output is correct |
8 |
Correct |
11 ms |
10784 KB |
Output is correct |
9 |
Correct |
11 ms |
10840 KB |
Output is correct |
10 |
Correct |
11 ms |
10912 KB |
Output is correct |
11 |
Correct |
11 ms |
10932 KB |
Output is correct |
12 |
Correct |
16 ms |
11596 KB |
Output is correct |
13 |
Correct |
16 ms |
11784 KB |
Output is correct |
14 |
Correct |
17 ms |
12292 KB |
Output is correct |
15 |
Correct |
18 ms |
12496 KB |
Output is correct |
16 |
Correct |
25 ms |
12576 KB |
Output is correct |
17 |
Correct |
20 ms |
12780 KB |
Output is correct |
18 |
Correct |
19 ms |
12780 KB |
Output is correct |
19 |
Correct |
19 ms |
12780 KB |
Output is correct |
20 |
Correct |
17 ms |
12780 KB |
Output is correct |
21 |
Correct |
21 ms |
12940 KB |
Output is correct |