답안 #223627

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
223627 2020-04-15T20:40:32 Z MKopchev 괄호 문자열 (CEOI16_match) C++14
0 / 100
9 ms 10496 KB
#include<bits/stdc++.h>
using namespace std;

const int nmax=1e5+42;

int n;
string inp;

char output[nmax];

int prev_pos[nmax][26];

void solve(int l,int r)
{
    if(l>r)return;

    int mid=prev_pos[r][inp[l]-'a'];

    if(l>=mid)
    {
        cout<<-1<<endl;
        exit(0);
    }

    output[l]='(';
    output[mid]=')';

    solve(l+1,mid-1);

    solve(mid+1,r);
}
int main()
{
    cin>>inp;
    n=inp.size();

    memset(prev_pos,-1,sizeof(prev_pos));

    for(int i=1;i<=n;i++)
        for(int j=0;j<26;j++)
    {
        if(inp[i]==j+'a')prev_pos[i][j]=i;
        else if(prev_pos[i-1][j]!=-1)prev_pos[i][j]=prev_pos[prev_pos[i-1][inp[i]-'a']-1][j];
    }

    solve(0,n-1);

    for(int i=0;i<n;i++)
        cout<<output[i];

    cout<<endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 10496 KB Output is correct
2 Incorrect 9 ms 10496 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 10496 KB Output is correct
2 Incorrect 9 ms 10496 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 10496 KB Output is correct
2 Incorrect 9 ms 10496 KB Output isn't correct
3 Halted 0 ms 0 KB -