답안 #388772

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
388772 2021-04-12T22:51:01 Z Ahmad_Hasan 괄호 문자열 (CEOI16_match) C++17
10 / 100
1 ms 452 KB
#include <bits/stdc++.h>

using namespace std;

int prf[26][(int)1e5+5];

string out;
string s;
int n;
int f=0;

bool check(int l,int r){
    if(l>r)return true;
    for(int i=0;i<26;i++){
        if((prf[i][r+1]-prf[i][l])&1)return false;
    }
    return true;
}

void slv(int l=0,int r=n-1){

    if(l>r)return;
    int sf=0;
    int i=0;
    for(i=r;i>l;i--){
        if(s[i]==s[l]&&check(l+1,i-1)){
            sf=1;
            out[l]='(';
            out[i]=')';
            slv(l+1,i-1);
            break;
        }
    }
    if(!sf){
        f=1;
        return;
    }

    slv(i+1,r);


}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);      cout.tie(0);

    cin>>s;
    n=s.size();

    for(int i=0;i<26;i++)prf[i][0]=0;

    for(int i=0;i<n;i++){
        for(int j=0;j<26;j++){
            prf[j][i+1]=prf[j][i]+(j==s[i]-'a');
        }
    }

    out=s;
    slv();
    if(f)
        cout<<-1<<'\n';
    else
        cout << out << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Incorrect 1 ms 452 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Incorrect 1 ms 452 KB Output isn't correct
5 Halted 0 ms 0 KB -