Submission #681413

# Submission time Handle Problem Language Result Execution time Memory
681413 2023-01-13T03:48:08 Z vjudge1 Match (CEOI16_match) C++14
10 / 100
0 ms 212 KB
#include <bits/stdc++.h>
using namespace std;
 
#define F first
#define S second
#define pb push_back
#define all(a) a.begin(), a.end()
 
typedef long long ll;
typedef pair<int, int> ii;
 
const int N = 2000 + 5;
const int mod = 1e9 + 7;

vector<int> pos['z' + 1];
string s;
string t;
int n;
 
void rec(int l, int r)
{
    if(l > r) return;
    int mid = *prev(upper_bound(all(pos[s[l]]), r));
    
    // assert(mid >= 0);
    
    t[l] = '(', t[mid] = ')';
    rec(l + 1, mid - 1);
    rec(mid + 1, r);
}
 
void solve()
{
    cin >> s;
    int n = s.size();
    t.resize(n);
    
    stack<char> st;
    for(int i = 0; i < n; i++)
    {
        if(st.size())
        {
            if(st.top() == s[i])
                st.pop();
            else
                st.push(s[i]);
        } else st.push(s[i]);
    }
    if(st.size()) cout << -1, exit(0);
    
    for(int i = 0; i < n; i++)
        pos[s[i]].pb(i);
    rec(0, n - 1);
    cout << t;
}
 
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message

match.cpp: In function 'void rec(int, int)':
match.cpp:23:45: warning: array subscript has type 'char' [-Wchar-subscripts]
   23 |     int mid = *prev(upper_bound(all(pos[s[l]]), r));
      |                                             ^
match.cpp:7:16: note: in definition of macro 'all'
    7 | #define all(a) a.begin(), a.end()
      |                ^
match.cpp:23:45: warning: array subscript has type 'char' [-Wchar-subscripts]
   23 |     int mid = *prev(upper_bound(all(pos[s[l]]), r));
      |                                             ^
match.cpp:7:27: note: in definition of macro 'all'
    7 | #define all(a) a.begin(), a.end()
      |                           ^
match.cpp: In function 'void solve()':
match.cpp:52:17: warning: array subscript has type 'char' [-Wchar-subscripts]
   52 |         pos[s[i]].pb(i);
      |                 ^
# Verdict Execution time Memory Grader output
1 Correct 0 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 0 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 Incorrect 0 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 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 Incorrect 0 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -