답안 #116637

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
116637 2019-06-13T09:20:30 Z JohnTitor 괄호 문자열 (CEOI16_match) C++11
10 / 100
2 ms 384 KB
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for(int i=(j); i<=(k); i++)
#define FFOR(i, j, k) for(int i=(j); i<(k); i++)
#define DFOR(i, j, k) for(int i=(j); i>=(k); i--)
#define bug(x) cerr<<#x<<" = "<<(x)<<'\n'
#define pb push_back
#define mp make_pair
#define bit(s, i) (((s)>>(i))&1LL)
#define mask(i) ((1LL<<(i)))
#define builtin_popcount __builtin_popcountll
#define __builtin_popcount __builtin_popcountll
using ll=long long; using ld=long double;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const ld pi=acos(0)*2;
template <typename T> inline void read(T &x){char c; bool nega=0; while((!isdigit(c=getchar()))&&(c!='-')); if(c=='-'){nega=1; c=getchar();} x=c-48; while(isdigit(c=getchar())) x=x*10+c-48; if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10); putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){ putchar('-'); x=-x;} writep(x);}
template <typename T> inline void writeln(T x){write(x); putchar('\n');}
#define taskname "Match"
string s;
int cnt[256];
vector <int> pos[26][26];
string t;
vector <char> v;
void kill(){
    puts("-1");
    exit(0);
}
int find_last(char f, char s, int l, int r){
    if(pos[f][s].empty()) return -1;
    int low=0, high=pos[f][s].size()-1, mid, res=-1;
    while(low<=high){
        mid=(low+high)/2;
        if(pos[f][s][mid]<=r){
            res=mid;
            low=mid+1;
        }
        else high=mid-1;
    }
    if(res==-1) return -1;
    if(pos[f][s][res]<l) return -1;
    return pos[f][s][res];
}
int find_first(char f, char s, int l, int r){
    if(pos[f][s].empty()) return -1;
    int low=0, high=pos[f][s].size()-1, mid, res=-1;
    while(low<=high){
        mid=(low+high)/2;
        if(pos[f][s][mid]>=r){
            res=mid;
            high=mid-1;
        }
        else low=mid+1;
    }
    if(res==-1) return -1;
    if(pos[f][s][res]>r) return -1;
    return pos[f][s][res];
}
void make(int l, int r){
    if(l>r) return;
    if((r-l+1)%2) kill();
    t[l]='(';
    t[r]=')';
    if(s[l]==s[r]){
        make(l+1, r-1);
    }
    else{
        if(l+1==r) kill();
        int x=find_first(s[l], s[r], l+1, r-2);
        if(x==-1) kill();
        t[x]=')';
        t[x+1]='(';
        make(l+1, x-1);
        make(x+2, r-1);
    }
}
int main(){
    #ifdef Aria
        if(fopen(taskname".in", "r"))
            freopen(taskname".in", "r", stdin);
    #endif // Aria
    cin>>s;
    for(char &c: s) c-='a';
    for(char c: s) cnt[c]++;
    FFOR(c, 0, 26) if(cnt[c]%2){
        puts("-1");
        return 0;
    }
    FFOR(i, 1, s.size()) pos[s[i-1]][s[i]].pb(i-1);
    t=s;
    make(0, s.size()-1);
    cout<<t<<'\n';
}

Compilation message

match.cpp: In function 'int find_last(char, char, int, int)':
match.cpp:30:13: warning: array subscript has type 'char' [-Wchar-subscripts]
     if(pos[f][s].empty()) return -1;
             ^
match.cpp:30:16: warning: array subscript has type 'char' [-Wchar-subscripts]
     if(pos[f][s].empty()) return -1;
                ^
match.cpp:31:26: warning: array subscript has type 'char' [-Wchar-subscripts]
     int low=0, high=pos[f][s].size()-1, mid, res=-1;
                          ^
match.cpp:31:29: warning: array subscript has type 'char' [-Wchar-subscripts]
     int low=0, high=pos[f][s].size()-1, mid, res=-1;
                             ^
match.cpp:34:17: warning: array subscript has type 'char' [-Wchar-subscripts]
         if(pos[f][s][mid]<=r){
                 ^
match.cpp:34:20: warning: array subscript has type 'char' [-Wchar-subscripts]
         if(pos[f][s][mid]<=r){
                    ^
match.cpp:41:13: warning: array subscript has type 'char' [-Wchar-subscripts]
     if(pos[f][s][res]<l) return -1;
             ^
match.cpp:41:16: warning: array subscript has type 'char' [-Wchar-subscripts]
     if(pos[f][s][res]<l) return -1;
                ^
match.cpp:42:17: warning: array subscript has type 'char' [-Wchar-subscripts]
     return pos[f][s][res];
                 ^
match.cpp:42:20: warning: array subscript has type 'char' [-Wchar-subscripts]
     return pos[f][s][res];
                    ^
match.cpp: In function 'int find_first(char, char, int, int)':
match.cpp:45:13: warning: array subscript has type 'char' [-Wchar-subscripts]
     if(pos[f][s].empty()) return -1;
             ^
match.cpp:45:16: warning: array subscript has type 'char' [-Wchar-subscripts]
     if(pos[f][s].empty()) return -1;
                ^
match.cpp:46:26: warning: array subscript has type 'char' [-Wchar-subscripts]
     int low=0, high=pos[f][s].size()-1, mid, res=-1;
                          ^
match.cpp:46:29: warning: array subscript has type 'char' [-Wchar-subscripts]
     int low=0, high=pos[f][s].size()-1, mid, res=-1;
                             ^
match.cpp:49:17: warning: array subscript has type 'char' [-Wchar-subscripts]
         if(pos[f][s][mid]>=r){
                 ^
match.cpp:49:20: warning: array subscript has type 'char' [-Wchar-subscripts]
         if(pos[f][s][mid]>=r){
                    ^
match.cpp:56:13: warning: array subscript has type 'char' [-Wchar-subscripts]
     if(pos[f][s][res]>r) return -1;
             ^
match.cpp:56:16: warning: array subscript has type 'char' [-Wchar-subscripts]
     if(pos[f][s][res]>r) return -1;
                ^
match.cpp:57:17: warning: array subscript has type 'char' [-Wchar-subscripts]
     return pos[f][s][res];
                 ^
match.cpp:57:20: warning: array subscript has type 'char' [-Wchar-subscripts]
     return pos[f][s][res];
                    ^
match.cpp: In function 'int main()':
match.cpp:84:25: warning: array subscript has type 'char' [-Wchar-subscripts]
     for(char c: s) cnt[c]++;
                         ^
match.cpp:4:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define FFOR(i, j, k) for(int i=(j); i<(k); i++)
                                       ^
match.cpp:89:5: note: in expansion of macro 'FFOR'
     FFOR(i, 1, s.size()) pos[s[i-1]][s[i]].pb(i-1);
     ^~~~
match.cpp:89:36: warning: array subscript has type 'char' [-Wchar-subscripts]
     FFOR(i, 1, s.size()) pos[s[i-1]][s[i]].pb(i-1);
                                    ^
match.cpp:89:42: warning: array subscript has type 'char' [-Wchar-subscripts]
     FFOR(i, 1, s.size()) pos[s[i-1]][s[i]].pb(i-1);
                                          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Incorrect 2 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Incorrect 2 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -