Submission #914448

# Submission time Handle Problem Language Result Execution time Memory
914448 2024-01-22T07:16:20 Z 8pete8 Match (CEOI16_match) C++17
0 / 100
0 ms 348 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<float.h>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,ll>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
using namespace std;
#define int long long
#define double long double
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
const int mod=1e9+7,mxn=3e5,lg=30,inf=1e18,minf=-1e9,Mxn=100000;
string a;
bool can[2002][2002];
vector<int>pos[50];
int32_t main(){
    cin>>a;
    int n=a.size();
    for(int i=0;i<n;i++)pos[a[i]-'a'].pb(i);
    for(int i=0;i<=26;i++)reverse(all(pos[i]));
    for(int i=0;i<n;i++){
        stack<int>st;
        for(int j=i;j<n;j++){
            if(!st.empty()&&a[st.top()]==a[j])st.pop();
            else st.push(j);
            can[i][j]=st.empty();
        }
    }
    string ans;
    ans.resize(n,'?');
    for(int i=0;i<n;i++){
        if(ans[i]!='?')continue;
        int p=-1;
        for(auto j:pos[a[i]-'a']){
            if(ans[j]!='?')continue;
            if(i!=j-1&&can[i+1][j-1]==false)continue;
            p=j;
            break;
        }
        if(p==-1||i==p)return cout<<-1,0;
        ans[i]='(';
        ans[p]=')';
    }
    stack<int>st;
    for(int i=0;i<n;i++){
        if(ans[i]=='?')return cout<<-1,0;
        if(ans[i]==')'&&(st.empty()||st.top()!=0))return cout<<-1,0;
        else if(ans[i]=='(')st.push(0);
    }
    cout<<ans;

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -