답안 #389655

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
389655 2021-04-14T10:44:24 Z MilosMilutinovic 괄호 문자열 (CEOI16_match) C++14
0 / 100
1 ms 204 KB
#include <bits/stdc++.h>
using namespace std;
#define N 100005

char s[N];
vector <int> pos[2];

bool valid(char* s)
{
    int bal=0;
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]=='(') bal++;
        if(s[i]==')') bal--;
        if(bal<0) return false;
    }
    return bal==0;
}
int main()
{
    scanf("%s",&s);
    int n=strlen(s);
    for(int i=0;i<n;i++)
    {
        if(s[i]=='a') pos[0].push_back(i);
        else pos[1].push_back(i);
    }
    if(pos[0].size()&1||pos[1].size()&1)
    {
        printf("-1");
        return 0;
    }
    char ans[n];
    int x=(int)pos[0].size(),y=(int)pos[1].size();
    for(int i=0;i<x;i++)
    {
        if(i<x/2) ans[pos[0][i]]='(';
        else ans[pos[0][i]]=')';
    }
    for(int i=0;i<y;i++)
    {
        if(i<y/2) ans[pos[1][i]]='(';
        else ans[pos[1][i]]=')';
    }
    if(!valid(ans))
    {
        printf("-1");
        return 0;
    }
    stack<int> st;
    int matc[n];
    for(int i=0;i<n;i++)
    {
        if(ans[i]=='(') st.push(i);
        else
        {
            int j=st.top();
            st.pop();
            matc[j]=i;
            matc[i]=j;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(s[i]!=s[matc[i]])
        {
            printf("-1");
            return 0;
        }
    }
    for(int i=0;i<n;i++) printf("%c",ans[i]);
}

Compilation message

match.cpp: In function 'bool valid(char*)':
match.cpp:11:18: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for(int i=0;i<strlen(s);i++)
      |                 ~^~~~~~~~~~
match.cpp: In function 'int main()':
match.cpp:21:13: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[100005]' [-Wformat=]
   21 |     scanf("%s",&s);
      |            ~^  ~~
      |             |  |
      |             |  char (*)[100005]
      |             char*
match.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   21 |     scanf("%s",&s);
      |     ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -