Submission #52876

# Submission time Handle Problem Language Result Execution time Memory
52876 2018-06-27T05:55:44 Z 노영훈(#1382) Match (CEOI16_match) C++11
0 / 100
3 ms 612 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=100010, inf=2e9;

int n;

int cnt[MX][30];
char S[MX], R[MX];

vector<int> pos[30];

void NOPE(){
    cout<<-1;
    exit(0);
}


int V[2010][2010];

bool valid(int x, int y){
    int &res=V[x][y];
    if(res>=0) return res;
    if(S[x]!=S[y]){
        res=false;
        return res;
    }
    if(x+1==y){
        res=true;
        return res;
    }
    res=valid(x+1, y-1);

    for(int z=x+1; z+1<=y-1; z++){
        res|=valid(x+1,z)&&valid(z+1, y-1);
    }
    return res;
}


int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    cin>>(S+1);
    for(int i=1; S[i]!=0; i++) n=i;
    
    for(int i=1; i<=n; i++){
        int x=S[i]-'a';
        pos[x].push_back(i);
        for(int j=0; j<='z'-'a'; j++)
            cnt[i][j]=cnt[i-1][j];
        cnt[i][x]++;
    }
    for(int i=1; i<=n; i++)
        for(int j=i+1; j<=n; j++)
            V[i][j]=-1;

    // for(int i=1; i<=n; i++, cout<<'\n')
    //     for(int j=i+1; j<=n; j++)
    //         cout<<valid(i,j)<<' ';

    for(int i=0; i<='z'-'a'; i++){
        int sz=pos[i].size();
        for(int j=0; j<sz; j++){
            int x=pos[i][j];
            if(R[x]!=0) continue;
            for(int k=sz-1; k>=0; k--){
                int y=pos[i][k];
                if(R[y]!=0) continue;
                if(y<=x) break;
                if(valid(x,y)){
                    R[x]='('; R[y]=')';
                    break;
                }
            }
        }
    }
    for(int i=1; i<=n; i++) if(R[i]==0) NOPE();
    cout<<(R+1);
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 3 ms 612 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 3 ms 612 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 3 ms 612 KB Output isn't correct
3 Halted 0 ms 0 KB -