답안 #745427

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
745427 2023-05-20T05:59:06 Z Tenis0206 괄호 문자열 (CEOI16_match) C++11
0 / 100
2 ms 340 KB
#include <bits/stdc++.h>

using namespace std;

const int nmax = 1e5;

int n;
int v[nmax + 5];

bool ok;

char rez[nmax + 5];

int fr[nmax + 5];

void solve(int st, int dr)
{
    if(st > dr)
    {
        return;
    }
    int poz = 0;
    for(int i=st+1;i<=dr;i++)
    {
        if(v[i]==v[st])
        {
            poz = i;
        }
    }
    if(!poz)
    {
        ok = false;
        return;
    }
    rez[st] = '(';
    rez[poz] = ')';
    solve(st+1,poz-1);
    solve(poz+1,dr);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("match.in","r",stdin);
    freopen("match.out","w",stdout);
    string s;
    cin>>s;
    n = s.size();
    for(int i=1;i<=n;i++)
    {
        v[i] = s[i - 1];
        ++fr[v[i]];
    }
    ok = true;
    for(int i=1;i<=n;i++)
    {
        if(fr[v[i]] % 2 == 1)
        {
            ok = false;
        }
    }
    if(!ok)
    {
        cout<<-1<<'\n';
        return 0;
    }
    solve(1,n);
    if(!ok)
    {
        cout<<-1<<'\n';
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        cout<<rez[i];
    }
    cout<<'\n';
    return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:45:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |     freopen("match.in","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
match.cpp:46:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |     freopen("match.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -