#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;
void f(int l, int r, string &s, string &t, vector<vector<int>> &last)
{
if(l > r) return;
int pos = last[r][s[l] - 'a'];
t[l] = '(', t[pos] = ')';
f(l + 1, pos - 1, s, t, last), f(pos + 1, r, s, t, last);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
vector<vector<int>> last(s.size(), vector<int>(26, -1));
stack<int> st;
for(int i = 0; i < s.size(); i++)
{
if(!st.empty() && s[st.top()] == s[i]) st.pop();
else st.push(i);
}
if(!st.empty())
{
cout << -1;
return 0;
}
for(int i = 0; i < s.size(); i++)
{
last[i][s[i] - 'a'] = i;
for(int j = 0; j < 26 && i > 0; j++)
{
last[i][j] = max(last[i - 1][j], last[i][j]);
}
}
string t(s.size(), ' ');
f(0, s.size() - 1, s, t, last);
cout << t;
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:24:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
match.cpp:36:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |