# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
749623 |
2023-05-28T10:16:02 Z |
박상훈(#9965) |
Match (CEOI16_match) |
C++17 |
|
2000 ms |
944 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
char a[100100], ans[100100];
bool ok(int s, int e, vector<int> st){
if (e-s < -1) return 0;
for (int i=s;i<=e;i++){
if (!st.empty() && st.back()==a[i]) st.pop_back();
else st.push_back(a[i]);
}
return st.empty();
}
int main(){
scanf("%s", a+1);
n = strlen(a+1);
if (!ok(1, n, vector<int>())){
printf("-1\n");
return 0;
}
vector<int> st;
vector<pair<int, int>> st2;
for (int i=1,j=n;i<=n;i++){
st.push_back(a[i]);
auto st20 = st2;
while(j>0){
if (st.size()==st2.size()+1 && a[j]==a[i]){
st2.emplace_back(a[j], j);
j--;
break;
}
else if (st.size()==st2.size()+1 || st2.back().first!=a[j]){
st2.emplace_back(a[j], j);
j--;
}
else{
st2.pop_back();
j--;
}
}
if (!ok(i+1, j, vector<int>())){
st.pop_back();
st2 = st20;
assert(!st.empty() && st.back()==a[i]);
st.pop_back();
st2.pop_back();
j = st2.empty()?n:(st2.back().second-1);
ans[i] = ')';
}
else ans[i] = '(';
// printf("i = %d -> j = %d\n", i, j);
// for (auto &x:st) printf("%d ", x-'a'+1);
// printf("\n");
// for (auto &[x, y]:st2) printf("(%d, %d) ", x-'a'+1, y);
// printf("\n");
}
ans[n+1] = 0;
printf("%s\n", ans+1);
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:20:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%s", a+1);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
308 KB |
Output is correct |
7 |
Correct |
2 ms |
244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
308 KB |
Output is correct |
7 |
Correct |
2 ms |
244 KB |
Output is correct |
8 |
Correct |
5 ms |
340 KB |
Output is correct |
9 |
Correct |
45 ms |
340 KB |
Output is correct |
10 |
Correct |
36 ms |
388 KB |
Output is correct |
11 |
Correct |
64 ms |
436 KB |
Output is correct |
12 |
Execution timed out |
2091 ms |
944 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |