Submission #608847

# Submission time Handle Problem Language Result Execution time Memory
608847 2022-07-27T10:49:52 Z faruk Match (CEOI16_match) C++17
100 / 100
1546 ms 2644 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
#define ld long double

using namespace std;


int n; string s;
bool possible(stack<char> open, int idx) {
    for (int i = idx; i < n; i++) {
        if (!open.empty() && open.top() == s[i])
            open.pop();
        else
            open.push(s[i]);
    }
    return open.empty();
}

int gaming_calc(char c, int endd) {
    stack<char> open;
    for (int i = endd; i >= 0; i--) {
        if (open.empty() && s[i] == c)
            return i;
        
        if (!open.empty() && open.top() == s[i])
            open.pop();
        else
            open.push(s[i]);
        
        if (open.empty() && s[i] == c)
            return i;
    }
    return -1;
}

int gaming[100001][30];
void tle_machine() {
    string t = s;
    sort(t.begin(), t.end());
    for (int i = 0; i < n; i++) {
        char x;
        if (i == 0 || t[i] != t[i - 1])
            x = t[i];
        for (int j = n - 1; j >= 0; j--)
            gaming[j][x] = gaming_calc(x, j);
    }
}

string out;
void recur(int strt, int e) {
    if (strt >= e)
        return;
    int mid = gaming_calc(s[strt], e);
    out[strt] = '(', out[mid] = ')';
    recur(strt + 1, mid - 1);
    recur(mid + 1, e);
}

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

    //freopen("match.in", "r", stdin);
    //freopen("match.out", "w", stdout);

    cin >> s;
    n = s.size();
    if (!possible(stack<char>(), 0)) {
        cout << "-1\n";
        return 0;
    }

    out = s;
    recur(0, n - 1);

    cout << out << "\n";

    return 0;
}

Compilation message

match.cpp: In function 'void tle_machine()':
match.cpp:46:23: warning: array subscript has type 'char' [-Wchar-subscripts]
   46 |             gaming[j][x] = gaming_calc(x, j);
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 7 ms 336 KB Output is correct
10 Correct 3 ms 340 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 72 ms 1568 KB Output is correct
13 Correct 34 ms 1652 KB Output is correct
14 Correct 483 ms 1952 KB Output is correct
15 Correct 5 ms 2644 KB Output is correct
16 Correct 6 ms 2644 KB Output is correct
17 Correct 56 ms 2624 KB Output is correct
18 Correct 13 ms 1476 KB Output is correct
19 Correct 762 ms 1880 KB Output is correct
20 Correct 299 ms 1696 KB Output is correct
21 Correct 1546 ms 2212 KB Output is correct