# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
749654 |
2023-05-28T10:48:57 Z |
박상훈(#9965) |
Match (CEOI16_match) |
C++17 |
|
37 ms |
27752 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, dp[100100][33], sp[100100][33], L[100100], R[100100];
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();
}
void init(){
if (!ok(1, n, vector<int>())){
printf("-1\n");
exit(0);
}
fill(L+1, L+n+1, -1);
fill(R+1, R+n+1, -1);
for (int i=0;i<=n;i++) fill(dp[i], dp[i]+26, -1);
for (int i=1;i<=n;i++){
L[i] = dp[i-1][a[i]-'a'];
if (L[i]==-1) R[i] = -1;
else R[L[i]] = i;
dp[i][a[i]-'a'] = i;
for (int j=0;j<26;j++){
if (j!=a[i]-'a' && L[i]!=-1){
dp[i][j] = dp[L[i]-1][j];
}
}
}
for (int i=1;i<=n;i++){
sp[i][0] = R[i];
}
for (int j=1;j<20;j++){
for (int i=1;i<=n;i++){
if (sp[i][j-1]!=-1 && sp[i][j-1] + 1 <= n){
sp[i][j] = sp[sp[i][j-1] + 1][j-1];
}
else sp[i][j] = -1;
}
}
// printf(" %d\n", sp[4][0]);
}
bool valid(int l, int r){
if (r-l+1 < 0) return 0;
for (int j=19;j>=0;j--) if (l<=n && sp[l][j]!=-1 && sp[l][j] <= r){
l = sp[l][j] + 1;
}
return r-l+1 == 0;
}
int main(){
scanf("%s", a+1);
n = strlen(a+1);
init();
vector<int> st;
vector<pair<int, int>> st2;
for (int i=1,j=n;i<=n;i++){
st.push_back(a[i]);
int nj = dp[j][a[i]-'a'];
// printf("\ni = a%d -> nj = %d\n", i, nj);
if (valid(i+1, nj-1)){
// printf("push ok\n");
ans[i] = '(';
st2.emplace_back(a[nj], nj);
j = nj-1;
}
else{
// printf("pop\n");
ans[i] = ')';
st.pop_back();
assert(!st.empty() && st.back()==a[i]);
st.pop_back();
st2.pop_back();
j = st2.empty()?n:(st2.back().second-1);
}
// 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:71:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%s", a+1);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
820 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2260 KB |
Output is correct |
10 |
Correct |
2 ms |
2260 KB |
Output is correct |
11 |
Correct |
2 ms |
2260 KB |
Output is correct |
12 |
Correct |
17 ms |
16760 KB |
Output is correct |
13 |
Correct |
17 ms |
18144 KB |
Output is correct |
14 |
Correct |
25 ms |
19596 KB |
Output is correct |
15 |
Correct |
31 ms |
22344 KB |
Output is correct |
16 |
Correct |
30 ms |
22344 KB |
Output is correct |
17 |
Correct |
33 ms |
23760 KB |
Output is correct |
18 |
Correct |
32 ms |
24828 KB |
Output is correct |
19 |
Correct |
32 ms |
26300 KB |
Output is correct |
20 |
Correct |
18 ms |
16868 KB |
Output is correct |
21 |
Correct |
37 ms |
27752 KB |
Output is correct |