Submission #1117865

#TimeUsernameProblemLanguageResultExecution timeMemory
1117865vjudge1Match (CEOI16_match)C++17
100 / 100
28 ms26704 KiB
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;

void f(int l, int r, string &s, string &t, vector<vector<int>> &last)
{
    if(l > r) return;
    int pos = last[r][s[l] - 'a'];
    t[l] = '(', t[pos] = ')';
    f(l + 1, pos - 1, s, t, last), f(pos + 1, r, s, t, last);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    string s;
    cin >> s;

    vector<vector<int>> last(s.size(), vector<int>(26, -1));
    stack<int> st;
    for(int i = 0; i < s.size(); i++)
    {
        if(!st.empty() && s[st.top()] == s[i]) st.pop();
        else st.push(i);
    }

    if(!st.empty())
    {
        cout << -1;
        return 0;
    }

    for(int i = 0; i < s.size(); i++)
    {
        last[i][s[i] - 'a'] = i;
        for(int j = 0; j < 26 && i > 0; j++)
        {
            int prv = last[i - 1][s[i] - 'a'];
            if(prv > 0 && s[i] - 'a' != j)
            {
                last[i][j] = last[prv - 1][j];
            }
        }
    }

    string t(s.size(), ' ');
    f(0, s.size() - 1, s, t, last);

    cout << t;
}

Compilation message (stderr)

match.cpp: In function 'int main()':
match.cpp:24:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i = 0; i < s.size(); i++)
      |                    ~~^~~~~~~~~~
match.cpp:36:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for(int i = 0; i < s.size(); i++)
      |                    ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...