Submission #609326

# Submission time Handle Problem Language Result Execution time Memory
609326 2022-07-27T13:51:10 Z Minindu2006 Match (CEOI16_match) C++14
37 / 100
2000 ms 24984 KB
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define len(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fst first
#define nl endl
#define sec second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ins insert
const int MOD = 1000000007;
const int MX = INT_MAX;
const int MN = INT_MIN;
void init()
{
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
#endif
}
void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
}
string s, ans;
void findSeq(int l, int r)
{
    if (r - l <= 0)
        return;
    int i;
    stack<char> st;

    // End index
    for (i = r; i > l; i--)
    {
        if(s[i] == s[l] && len(st) == 0)
            break;
        if(len(st) > 0 && st.top() == s[i])
            st.pop();
        else
            st.push(s[i]);
        if(s[i] == s[l] && len(st) == 0)
            break;
    }
    ans[l] = '(', ans[i] = ')';
    findSeq(l + 1, i - 1);
    findSeq(i + 1, r);
}

void solve()
{
    cin >> s;
    int n = len(s);

    if (n % 2)
    {
        cout << -1;
        return;
    }

    // Is it possible to create a bracket sequence
    stack<char> st;
    for (int i = 0; i < n; i++)
    {
        if (!st.empty() && st.top() == s[i])
            st.pop();
        else
            st.push(s[i]);
    }

    if (len(st) > 0)
    {
        cout << -1;
        return;
    }
    ans.resize(n, 'X');
    findSeq(0, n - 1);
    cout << ans << '\n';
}
int main()
{
    fastIO();
    
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
    return 0;
}

Compilation message

match.cpp: In function 'void init()':
match.cpp:38:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |     freopen("input.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     freopen("output.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 468 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 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 3 ms 468 KB Output is correct
9 Correct 14 ms 1748 KB Output is correct
10 Correct 7 ms 1476 KB Output is correct
11 Correct 4 ms 2516 KB Output is correct
12 Correct 140 ms 12804 KB Output is correct
13 Correct 65 ms 14040 KB Output is correct
14 Correct 763 ms 21396 KB Output is correct
15 Correct 19 ms 24460 KB Output is correct
16 Correct 22 ms 24524 KB Output is correct
17 Correct 107 ms 24984 KB Output is correct
18 Correct 15 ms 10196 KB Output is correct
19 Correct 1209 ms 19540 KB Output is correct
20 Correct 510 ms 18292 KB Output is correct
21 Execution timed out 2055 ms 16464 KB Time limit exceeded
22 Halted 0 ms 0 KB -