Submission #609335

# Submission time Handle Problem Language Result Execution time Memory
609335 2022-07-27T13:59:42 Z Minindu2006 Match (CEOI16_match) C++14
37 / 100
2000 ms 24908 KB
#include <bits/stdc++.h>
using namespace std;
 
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] && st.empty())
            break;
        if(!st.empty() && st.top() == s[i])
            st.pop();
        else
            st.push(s[i]);
        if(s[i] == s[l] && st.empty())
            break;
    }
    ans[l] = '(', ans[i] = ')';
    findSeq(l + 1, i - 1);
    findSeq(i + 1, r);
}
 
void solve()
{
    cin >> s;
    int n = s.size();
 
    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 (!st.empty())
    {
        cout << -1;
        return;
    }
    ans.resize(n, 'X');
    findSeq(0, n - 1);
    cout << ans;
}
int main()
{    
 solve();
}
# 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 0 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 0 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 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 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 13 ms 1728 KB Output is correct
10 Correct 6 ms 1476 KB Output is correct
11 Correct 4 ms 2516 KB Output is correct
12 Correct 123 ms 12696 KB Output is correct
13 Correct 61 ms 13984 KB Output is correct
14 Correct 694 ms 21164 KB Output is correct
15 Correct 21 ms 24404 KB Output is correct
16 Correct 22 ms 24356 KB Output is correct
17 Correct 96 ms 24908 KB Output is correct
18 Correct 16 ms 10068 KB Output is correct
19 Correct 1113 ms 19420 KB Output is correct
20 Correct 478 ms 18268 KB Output is correct
21 Execution timed out 2086 ms 20708 KB Time limit exceeded
22 Halted 0 ms 0 KB -