#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++)
{
int prv = last[i - 1][s[i] - 'a'];
if(prv > 0 && s[i] - 'a' != j)
{
last[i][j] = last[prv - 1][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++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
848 KB |
Output is correct |
7 |
Correct |
1 ms |
848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
848 KB |
Output is correct |
7 |
Correct |
1 ms |
848 KB |
Output is correct |
8 |
Correct |
2 ms |
1872 KB |
Output is correct |
9 |
Correct |
3 ms |
2128 KB |
Output is correct |
10 |
Correct |
2 ms |
2128 KB |
Output is correct |
11 |
Correct |
3 ms |
2128 KB |
Output is correct |
12 |
Correct |
15 ms |
16208 KB |
Output is correct |
13 |
Correct |
17 ms |
17488 KB |
Output is correct |
14 |
Correct |
22 ms |
19024 KB |
Output is correct |
15 |
Correct |
21 ms |
22096 KB |
Output is correct |
16 |
Correct |
20 ms |
22096 KB |
Output is correct |
17 |
Correct |
28 ms |
23376 KB |
Output is correct |
18 |
Correct |
21 ms |
23376 KB |
Output is correct |
19 |
Correct |
25 ms |
25168 KB |
Output is correct |
20 |
Correct |
15 ms |
16464 KB |
Output is correct |
21 |
Correct |
25 ms |
26704 KB |
Output is correct |