#include <bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 100050
int lastpos[maxn][26];
string input;
vector<int> out(maxn,0);
int right(int r,char c){
if(lastpos[r][c-'a']!=-1){
//return lastpos[r][c-'a'];
}
int ret = -1;
if(r-2>=0&&input.at(r)==input.at(r-1)){
if(input.at(r-2)==c){
ret = r-2;
}
else{
ret = right(r-2,c);
}
}
else{
int pos = right(r-1,input.at(r))-1;;
if(input.at(pos)==c){
ret = pos;
}
else{
ret = right(pos,c);
}
}
lastpos[r][c-'a'] = ret;
return ret;
}
void recursive(int l,int r){
if(l>=r){
return;
}
if(input.at(l)==input.at(r)){
out[r] = 1;
recursive(l+1,r-1);
return;
}
int pos = right(r,input.at(l));
out[pos] = 1;
recursive(l+1,pos-1);
recursive(pos+1,r);
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> input;
stack<int> stack1;
stack1.push(0);
for(int i=1;i<input.length();i++){
if(stack1.size()!=0&&input.at(stack1.top())==input.at(i)){
stack1.pop();
}
else{
stack1.push(i);
}
}
if(stack1.size()!=0){
cout << -1;
return 0;
}
for(int i=0;i<26;i++){
for(int j=0;j<input.length();j++){
lastpos[j][i] = -1;
}
}
recursive(0,input.length()-1);
for(int i=0;i<input.length();i++){
if(out[i]==1){
cout << ')';
}
else{
cout << '(';
}
}
}
Compilation message
match.cpp: In function 'int32_t main()':
match.cpp:53:16: 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]
53 | for(int i=1;i<input.length();i++){
| ~^~~~~~~~~~~~~~~
match.cpp:66:18: 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]
66 | for(int j=0;j<input.length();j++){
| ~^~~~~~~~~~~~~~~
match.cpp:71:16: 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]
71 | for(int i=0;i<input.length();i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1312 KB |
Output is correct |
5 |
Correct |
1 ms |
1280 KB |
Output is correct |
6 |
Correct |
1 ms |
1408 KB |
Output is correct |
7 |
Correct |
2 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
1 ms |
1152 KB |
Output is correct |
3 |
Correct |
1 ms |
1152 KB |
Output is correct |
4 |
Correct |
1 ms |
1312 KB |
Output is correct |
5 |
Correct |
1 ms |
1280 KB |
Output is correct |
6 |
Correct |
1 ms |
1408 KB |
Output is correct |
7 |
Correct |
2 ms |
1536 KB |
Output is correct |
8 |
Correct |
3 ms |
2432 KB |
Output is correct |
9 |
Correct |
14 ms |
2688 KB |
Output is correct |
10 |
Correct |
7 ms |
2688 KB |
Output is correct |
11 |
Correct |
5 ms |
2688 KB |
Output is correct |
12 |
Correct |
142 ms |
14336 KB |
Output is correct |
13 |
Correct |
74 ms |
15488 KB |
Output is correct |
14 |
Correct |
899 ms |
16888 KB |
Output is correct |
15 |
Correct |
25 ms |
17784 KB |
Output is correct |
16 |
Correct |
24 ms |
17784 KB |
Output is correct |
17 |
Correct |
108 ms |
19576 KB |
Output is correct |
18 |
Correct |
33 ms |
19832 KB |
Output is correct |
19 |
Correct |
1442 ms |
21844 KB |
Output is correct |
20 |
Correct |
601 ms |
14712 KB |
Output is correct |
21 |
Execution timed out |
2083 ms |
23164 KB |
Time limit exceeded |