Submission #444263

#TimeUsernameProblemLanguageResultExecution timeMemory
444263prvocisloMatch (CEOI16_match)C++17
100 / 100
24 ms15452 KiB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

void add(vector<char> &st, char c)
{
    if (!st.empty() && st.back() == c) st.pop_back();
    else st.push_back(c);
}
const int maxn = 1e5 + 5, abc = 26;
//const int maxn = 15, abc = 2;
vector<vector<int> > pv(maxn, vector<int>(abc, -1));
string ans, s;
void rek(int l, int r) // vratane
{
    if (l > r) return;
    int m = pv[r][s[l]-'a'];
    assert(l < m);
    ans[l] = '(', ans[m] = ')';
    rek(l+1, m-1), rek(m+1, r);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> s;
    int n = s.size();
    vector<char> st;
    for (int i = 0; i < n; i++)
        add(st, s[i]);
    if (!st.empty())
    {
        cout << "-1\n";
        return 0;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < abc; j++)
        {
            if (s[i]-'a' == j) pv[i][j] = i;
            else if (i && pv[i-1][s[i]-'a'] > 0) pv[i][j] = pv[pv[i-1][s[i]-'a']-1][j];
            //cout << pv[i][j] << " ";
        }
        //cout << endl;
    }
    ans.resize(n, '?');
    rek(0, n-1);
    cout << ans << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...