Submission #681415

# Submission time Handle Problem Language Result Execution time Memory
681415 2023-01-13T03:56:55 Z vjudge1 Match (CEOI16_match) C++14
100 / 100
29 ms 50360 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 = 1e5 + 5;
const int mod = 1e9 + 7;

int lst[N]['z' + 1];
string s;
string t;
int n;
 
void rec(int l, int r)
{
    if(l > r) return;
    int mid = lst[r][s[l]];
    
    // 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++)
    {
        lst[i][s[i]] = i;
        if(i == 0) continue;
        
        int j = lst[i - 1][s[i]];
        for(char c = 'a'; c <= 'z'; c++)
            if(j > 0 && c != s[i]) lst[i][c] = lst[j - 1][c];
    }
    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:26: warning: array subscript has type 'char' [-Wchar-subscripts]
   23 |     int mid = lst[r][s[l]];
      |                          ^
match.cpp: In function 'void solve()':
match.cpp:53:20: warning: array subscript has type 'char' [-Wchar-subscripts]
   53 |         lst[i][s[i]] = i;
      |                    ^
match.cpp:56:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   56 |         int j = lst[i - 1][s[i]];
      |                                ^
match.cpp:58:43: warning: array subscript has type 'char' [-Wchar-subscripts]
   58 |             if(j > 0 && c != s[i]) lst[i][c] = lst[j - 1][c];
      |                                           ^
match.cpp:58:59: warning: array subscript has type 'char' [-Wchar-subscripts]
   58 |             if(j > 0 && c != s[i]) lst[i][c] = lst[j - 1][c];
      |                                                           ^
# 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 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 1236 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 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 1236 KB Output is correct
8 Correct 2 ms 3412 KB Output is correct
9 Correct 2 ms 3788 KB Output is correct
10 Correct 2 ms 3796 KB Output is correct
11 Correct 2 ms 3796 KB Output is correct
12 Correct 18 ms 30476 KB Output is correct
13 Correct 19 ms 32964 KB Output is correct
14 Correct 20 ms 35740 KB Output is correct
15 Correct 21 ms 41212 KB Output is correct
16 Correct 20 ms 41088 KB Output is correct
17 Correct 25 ms 43564 KB Output is correct
18 Correct 23 ms 44732 KB Output is correct
19 Correct 27 ms 47580 KB Output is correct
20 Correct 18 ms 30668 KB Output is correct
21 Correct 29 ms 50360 KB Output is correct