답안 #223629

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
223629 2020-04-15T20:45:24 Z MKopchev 괄호 문자열 (CEOI16_match) C++14
100 / 100
27 ms 12032 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=0;i<n;i++)
        for(int j=0;j<26;j++)
    {
        if(inp[i]==j+'a')prev_pos[i][j]=i;
        else if(i&&prev_pos[i-1][inp[i]-'a']>0)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 Correct 9 ms 10496 KB Output is correct
3 Correct 9 ms 10496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 10496 KB Output is correct
2 Correct 9 ms 10496 KB Output is correct
3 Correct 9 ms 10496 KB Output is correct
4 Correct 9 ms 10496 KB Output is correct
5 Correct 9 ms 10496 KB Output is correct
6 Correct 10 ms 10496 KB Output is correct
7 Correct 9 ms 10496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 10496 KB Output is correct
2 Correct 9 ms 10496 KB Output is correct
3 Correct 9 ms 10496 KB Output is correct
4 Correct 9 ms 10496 KB Output is correct
5 Correct 9 ms 10496 KB Output is correct
6 Correct 10 ms 10496 KB Output is correct
7 Correct 9 ms 10496 KB Output is correct
8 Correct 11 ms 10624 KB Output is correct
9 Correct 10 ms 10624 KB Output is correct
10 Correct 12 ms 10624 KB Output is correct
11 Correct 10 ms 10624 KB Output is correct
12 Correct 19 ms 11264 KB Output is correct
13 Correct 21 ms 11392 KB Output is correct
14 Correct 22 ms 11648 KB Output is correct
15 Correct 24 ms 11904 KB Output is correct
16 Correct 23 ms 11904 KB Output is correct
17 Correct 25 ms 12032 KB Output is correct
18 Correct 26 ms 11368 KB Output is correct
19 Correct 26 ms 11648 KB Output is correct
20 Correct 20 ms 11392 KB Output is correct
21 Correct 27 ms 11760 KB Output is correct