Submission #476717

# Submission time Handle Problem Language Result Execution time Memory
476717 2021-09-28T09:55:35 Z wiktoria_bazan Match (CEOI16_match) C++14
37 / 100
2000 ms 588 KB
#include <iostream>
#include <stack>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

int n;
string s;
stack <char> S;

string generuj(string w, int in) {
    int sum = 0;
    for (int i = 0; i < in; i++) {
        if (w[i] == '(') {
            sum++;
            S.push(s[i]);
        }
        else {
            sum--;
            S.pop();
        }
    }
    w[in] = '(';
    S.push(s[in]);
    sum++;
    for (int i = in + 1; i < n; i++) {
        if (S.empty()) {
            sum++;
            w[i] = '(';
            S.push(s[i]);
        }
        else {
            char v = S.top();
            if (v == s[i]) {
                sum--;
                S.pop();
                w[i] = ')';
            }
            else {
                sum++;
                w[i] = '(';
                S.push(s[i]);
            }
        }
    }
    while (!S.empty()) S.pop();
    if (sum != 0) {
        return "))))))))))))))))))";
    }
    else return w;
}

void task() {
    int sum = 0;
    string w = "";
    for (int i = 0; i < n; i++) {
        if (S.empty()) {
            sum++;
            w.insert(w.size(), "(");
            S.push(s[i]);
        }
        else {
            char v = S.top();
            if (v == s[i]) {
                sum--;
                S.pop();
                w.insert(w.size(), ")");
            }
            else {
                sum++;
                w.insert(w.size(), "(");
                S.push(s[i]);
            }
        }
    }
    if (sum != 0) {
        cout << -1;
        return;
    }
    for (int i = 1; i < n; i++) {
        if (w[i] == ')') {
            w = min( w, generuj(w, i));
        }
    }
    cout << w;
    return;
}

int main()
{
    cin >> s;
    n = s.size();
    task();
   
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 4 ms 204 KB Output is correct
5 Correct 3 ms 204 KB Output is correct
6 Correct 10 ms 304 KB Output is correct
7 Correct 15 ms 304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 4 ms 204 KB Output is correct
5 Correct 3 ms 204 KB Output is correct
6 Correct 10 ms 304 KB Output is correct
7 Correct 15 ms 304 KB Output is correct
8 Correct 158 ms 204 KB Output is correct
9 Correct 192 ms 324 KB Output is correct
10 Correct 158 ms 304 KB Output is correct
11 Correct 165 ms 308 KB Output is correct
12 Execution timed out 2086 ms 588 KB Time limit exceeded
13 Halted 0 ms 0 KB -