Submission #923186

# Submission time Handle Problem Language Result Execution time Memory
923186 2024-02-06T20:04:25 Z JakobZorz Match (CEOI16_match) C++17
100 / 100
6 ms 11340 KB
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
#pragma GCC optimize ("03,unroll-loops")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
 
int n;
string str;
string res;
bool is_possible(){
    vector<char>p;
    for(int j=0;j<n;j++){
        if(p.empty()||p.back()!=str[j]){
            p.push_back(str[j]);
        }else{
            p.pop_back();
        }
    }
    
    return p.empty();
}

int dp[100000][26];

void solve(int l,int r){
    if(l>=r)
        return;
    
    int i=dp[r-1][str[l]-'a'];
    
    res[i]=')';
    res[l]='(';
    solve(l+1,i);
    solve(i+1,r);
}
 
void solve(){
    cin>>str;
    n=(int)str.size();
    if(!is_possible()){
        cout<<"-1\n";
        return;
    }
    
    for(int i=0;i<n;i++){
        for(int j=0;j<26;j++)
            dp[i][j]=-1;
        int ch=str[i]-'a';
        int pr=-1;
        if(i)
            pr=dp[i-1][ch];
        for(int j=0;j<26;j++){
            if(pr>0){
                if(str[pr-1]==j+'a')
                    dp[i][j]=pr-1;
                else
                    dp[i][j]=dp[pr-1][j];
            }
        }
        dp[i][ch]=i;
    }
    
    res=string(n,'-');
    solve(0,n);
    cout<<res<<"\n";
}
 
int main(){
    ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
    //freopen("input.in","r",stdin);freopen("output.out","w",stdout);
    int t=1;//cin>>t;
    while(t--)solve();
    return 0;
}
 
/*
 
abbaaa
(()())
 
abab
-1
 
 */
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 2516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 2516 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 3 ms 7260 KB Output is correct
13 Correct 3 ms 9304 KB Output is correct
14 Correct 3 ms 9400 KB Output is correct
15 Correct 4 ms 9564 KB Output is correct
16 Correct 6 ms 9712 KB Output is correct
17 Correct 5 ms 9820 KB Output is correct
18 Correct 5 ms 10076 KB Output is correct
19 Correct 5 ms 10844 KB Output is correct
20 Correct 3 ms 7260 KB Output is correct
21 Correct 6 ms 11340 KB Output is correct