Submission #923165

# Submission time Handle Problem Language Result Execution time Memory
923165 2024-02-06T19:02:18 Z JakobZorz Match (CEOI16_match) C++17
10 / 100
1 ms 348 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();
}

void solve(int l,int r){
    if(l>=r)
        return;
    for(int i=r-1;i>=l;i--){
        if(str[i]==str[l]){
            res[i]=')';
            res[l]='(';
            solve(l+1,i);
            solve(i+1,r);
            return;
        }
    }
}
 
void solve(){
    cin>>str;
    n=(int)str.size();
    if(!is_possible()){
        cout<<"-1\n";
        return;
    }
    res=string(n,'-');
    solve(0,n);
    for(char c:res)
        if(c=='-')
            abort();
    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 1 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 1 ms 348 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 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 1 ms 348 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -