Submission #613320

# Submission time Handle Problem Language Result Execution time Memory
613320 2022-07-30T04:31:36 Z drdilyor Match (CEOI16_match) C++17
100 / 100
51 ms 56576 KB
#include <bits/stdc++.h>
#ifdef ONPC
    #include "t_debug.cpp"
#else
    #define debug(...) 42
#endif
#define sz(a) ((int)(a).size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll INFL = 1e18;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.first >> p.second; }
const int N = 2e5, LOGN = 17, MOD = 1e9+7;

string solve(const string& s) {
    int n = s.size();
    string res(n, '?');
    vector<vector<int>> prev(n, vector<int>(128, -1));

    for (int i = 0; i < n; i++) {
        if (i) {
            int j = prev[i-1][s[i]] - 1;
            if (j >= 0) prev[i] = prev[j];
        }
        prev[i][s[i]] = i;
    }

    vector<char> cur;
    for (int i = 0; i < n; i++) {
        if (!cur.empty() && cur.back() == s[i]) {
            cur.pop_back();
        } else {
            cur.push_back(s[i]);
        }
    }
    if (!cur.empty()) return "-1";

    function<void(int,int)> construct = [&](int beg, int end) {
        if (beg > end) return;
        if (s[beg] == s[end]) {
            res[beg] = '(';
            res[end] = ')';
            construct(beg+1, end-1);
            return;
        }
        int j = prev[end][s[beg]];
        debug(s, beg, end, j);
        res[beg] = '(';
        res[j] = ')';
        construct(beg+1, j-1);
        construct(j+1, end);
    };
    construct(0, n-1);
    return res;
}

string solve_2(const string& s) {
    int n = s.size();
    string res(n, '?');
    function<bool(int)> brute = [&](int i) {
        if (i < n) {
            res[i] = '(';
            if (brute(i+1)) return true;
            res[i] = ')';
            return brute(i+1);
        } else {
            vector<int> matching(n, -1);
            vector<int> cur;
            for (int i = 0; i < n; i++) {
                if (res[i] == '(') {
                    cur.push_back(i);
                } else {
                    if (cur.empty()) return false;
                    if (s[i] != s[cur.back()]) return false; 
                    cur.pop_back();
                }
            }
            return cur.empty();
        }
    };
    return brute(0) ? res : "-1";
}

signed main() {
    int t = 1;
    #ifdef ONPC
    cin >> t;
    #endif
    while (t-- && cin) {
        string s; cin >> s;
        if (!cin) break;
        #ifdef ONPC
        cout << "Case " << s << '\n';
        string res1 = solve(s), res2 = solve_2(s);
        cout << res1 << endl;
        if (res1 != res2) {
            cout << res2 << endl;
            cout << "Case " << s << '\n';
            assert(false);
        }
        #else
        cout << solve(s);
#endif
    }
    return 0;
}

/*
     █████               █████  ███  ████                               
    ▒▒███               ▒▒███  ▒▒▒  ▒▒███                               
  ███████  ████████   ███████  ████  ▒███  █████ ████  ██████  ████████ 
 ███▒▒███ ▒▒███▒▒███ ███▒▒███ ▒▒███  ▒███ ▒▒███ ▒███  ███▒▒███▒▒███▒▒███
▒███ ▒███  ▒███ ▒▒▒ ▒███ ▒███  ▒███  ▒███  ▒███ ▒███ ▒███ ▒███ ▒███ ▒▒▒ 
▒███ ▒███  ▒███     ▒███ ▒███  ▒███  ▒███  ▒███ ▒███ ▒███ ▒███ ▒███     
▒▒████████ █████    ▒▒████████ █████ █████ ▒▒███████ ▒▒██████  █████    
 ▒▒▒▒▒▒▒▒ ▒▒▒▒▒      ▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒   ▒▒▒▒▒███  ▒▒▒▒▒▒  ▒▒▒▒▒     
                                            ███ ▒███                    
                                           ▒▒██████                     
                                            ▒▒▒▒▒▒
*/

Compilation message

match.cpp: In lambda function:
match.cpp:5:24: warning: statement has no effect [-Wunused-value]
    5 |     #define debug(...) 42
      |                        ^~
match.cpp:49:9: note: in expansion of macro 'debug'
   49 |         debug(s, beg, end, j);
      |         ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 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 2 ms 820 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 2 ms 820 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1324 KB Output is correct
8 Correct 4 ms 3764 KB Output is correct
9 Correct 5 ms 4180 KB Output is correct
10 Correct 5 ms 4144 KB Output is correct
11 Correct 5 ms 4328 KB Output is correct
12 Correct 39 ms 33820 KB Output is correct
13 Correct 40 ms 36748 KB Output is correct
14 Correct 33 ms 39992 KB Output is correct
15 Correct 45 ms 45692 KB Output is correct
16 Correct 40 ms 45632 KB Output is correct
17 Correct 44 ms 48492 KB Output is correct
18 Correct 51 ms 49940 KB Output is correct
19 Correct 47 ms 53324 KB Output is correct
20 Correct 36 ms 34324 KB Output is correct
21 Correct 51 ms 56576 KB Output is correct