Submission #236970

#TimeUsernameProblemLanguageResultExecution timeMemory
236970SortingMatch (CEOI16_match)C++14
37 / 100
2078 ms5248 KiB
#include <bits/stdc++.h>

using namespace std;

string s;
int n;

bool check(string &curr, int pos){
    if(curr == "" && pos == n)
        return true;
    if(curr.size() > (n - pos))
        return false;

    if(curr != "" && curr.back() == s[pos]){
        curr.pop_back();
        bool answer = check(curr, pos + 1);
        curr += s[pos];

        return answer;
    }

    curr += s[pos];
    bool answer = check(curr, pos + 1);
    curr.pop_back();

    return answer;
}

void solve(string &answer, int pos){
    if(answer == "" && pos == n)
        return;

    answer += s[pos];
    if(check(answer, pos + 1)){
        solve(answer, pos + 1);
        answer += "(";
        return;
    }
    answer.pop_back();

    if(answer != "" && answer.back() == s[pos]){
        answer.pop_back();
        solve(answer, pos + 1);
        answer += ")";
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

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

    string answer = "";
    if(!check(answer, 0)){
        cout << "-1\n";
        return 0;
    }

    solve(answer, 0);

    reverse(answer.begin(), answer.end());
    cout << answer << "\n";
}

Compilation message (stderr)

match.cpp: In function 'bool check(std::__cxx11::string&, int)':
match.cpp:11:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(curr.size() > (n - pos))
        ~~~~~~~~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...