Submission #745436

# Submission time Handle Problem Language Result Execution time Memory
745436 2023-05-20T06:34:13 Z Alexandruabcde Match (CEOI16_match) C++14
37 / 100
2000 ms 952 KB
#include <bits/stdc++.h>

using namespace std;

constexpr int NMAX = 1e5 + 5;
constexpr int SIGMAX = 26;

bool exist;

int sp[NMAX];
char ans[NMAX];

void Precompute (string S) {
    deque <char> D;
    for (int i = 0; i < S.size(); ++ i ) {
        if (D.empty() || D.back() != S[i]) {
            D.push_back(S[i]);
        }
        else {
            D.pop_back();
        }
    }

    exist = D.empty();
}

int pos[SIGMAX];
int bef[NMAX];

void Solve (string S) {
    for (int i = 0; i < SIGMAX; ++ i )
        pos[i] = -1;

    for (int i = 0; i < S.size(); ++ i ) {
        bef[i] = pos[S[i] - 'a'];

        pos[S[i] - 'a'] = i;
    }

    for (int i = 0; i < S.size(); ++ i ) {
        if (ans[i] == ')') continue;

        deque <char> D;
        int last = -1;

        for (int j = i+1; j < S.size(); ++ j ) {
            if (ans[j] == ')') {
                if (D.empty() || D.back() != S[j])
                    break;

                D.pop_back();
                continue;
            }

            if (D.empty() || D.back() != S[j]) {
                if (D.empty() && S[i] == S[j])
                    last = j;

                D.push_back(S[j]);
            }
            else {
                D.pop_back();
            }
        }

        ans[i] = '(';
        ans[last] = ')';
    }

    for (int i = 0; i < S.size(); ++ i )
        cout << ans[i];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    string S;
    cin >> S;

    Precompute(S);

    if (!exist) {
        cout << -1 << '\n';
        return 0;
    }

    Solve(S);

    return 0;
}

Compilation message

match.cpp: In function 'void Precompute(std::string)':
match.cpp:15:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     for (int i = 0; i < S.size(); ++ i ) {
      |                     ~~^~~~~~~~~~
match.cpp: In function 'void Solve(std::string)':
match.cpp:34:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     for (int i = 0; i < S.size(); ++ i ) {
      |                     ~~^~~~~~~~~~
match.cpp:40:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for (int i = 0; i < S.size(); ++ i ) {
      |                     ~~^~~~~~~~~~
match.cpp:46:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |         for (int j = i+1; j < S.size(); ++ j ) {
      |                           ~~^~~~~~~~~~
match.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for (int i = 0; i < S.size(); ++ i )
      |                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 4 ms 212 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 4 ms 212 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 5 ms 340 KB Output is correct
9 Correct 41 ms 372 KB Output is correct
10 Correct 26 ms 376 KB Output is correct
11 Correct 43 ms 368 KB Output is correct
12 Correct 1950 ms 952 KB Output is correct
13 Execution timed out 2061 ms 868 KB Time limit exceeded
14 Halted 0 ms 0 KB -