Submission #589155

# Submission time Handle Problem Language Result Execution time Memory
589155 2022-07-04T09:44:46 Z andrei_boaca Match (CEOI16_match) C++14
0 / 100
12 ms 19420 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
using namespace std;
typedef pair<int,int> pii;
int n;
string s,sol;
stack<char> st;
vector<int> mypoz[31],v;
set<int> par[100005],imp[100005];
map<int,int> nrm;
int nxt[100005];
int sum[100005];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>s;
    n=s.size();
    s=" "+s;
    if(n%2==1)
    {
        cout<<-1;
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        mypoz[s[i]-'a'+1].push_back(i);
        if(!st.empty()&&st.top()==s[i])
            st.pop();
        else
            st.push(s[i]);
    }
    if(!st.empty())
    {
        cout<<-1;
        return 0;
    }
    sol.resize(n+1);
    for(int i=1;i<=26;i++)
    {
        v=mypoz[i];
        int suma=0;
        nrm.clear();
        int nr=0;
        nxt[0]=v[0];
        nrm[0]=1;
        nr=1;
        for(int j=0;j<v.size();j++)
        {
            if(v[j]%2==1)
                suma++;
            else
                suma--;
            if(nrm.count(suma)==0)
            {
                nr++;
                nrm[suma]=nr;
            }
            /*if(v[j]%2==0)
                par[nrm[suma]].insert(v[j]);
            else
                imp[nrm[suma]].insert(v[j]);*/
            sum[v[j]]=suma;
        }
        suma=0;
        for(int j=0;j<v.size();j++)
            if(sol[v[j]]!=')')
            {
                sol[v[j]]='(';
                int poz=0;
                for(int k=j+1;k<v.size();k++)
                {
                    if(sol[v[k]]==')')
                        break;
                    if(sum[v[k]]==suma&&v[k]%2!=v[j]%2)
                        poz=v[k];
                }
                sol[poz]=')';
                if(v[j]%2==1)
                    suma++;
                else
                    suma--;
            }
        /*set<int> last;
        suma=0;
        for(int j=0;j<v.size();j++)
        {
            if(sol[v[j]]!=')')
            {
                sol[v[j]]='(';
                int maxpoz=n+1;
                if(!last.empty())
                    maxpoz=*last.begin();
                if(v[j]%2==0)
                {
                    auto it=prev(imp[nrm[suma]].upper_bound(maxpoz));
                    int poz=(*it);
                    sol[poz]=')';
                    last.insert(poz);
                    imp[nrm[suma]].erase(it);;
                }
                else
                {
                    auto it=prev(par[nrm[suma]].upper_bound(maxpoz));
                    int poz=(*it);
                    sol[poz]=')';
                    last.insert(poz);
                    par[nrm[suma]].erase(it);
                }
            }
            else
                last.erase(v[j]);
            if(v[j]%2==1)
                suma++;
            else
                suma--;
        }
        for(int j=1;j<=nr;j++)
        {
            par[j].clear();
            imp[j].clear();
        }*/
    }
    for(int i=1;i<=n;i++)
        cout<<sol[i];
    return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:48:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |         for(int j=0;j<v.size();j++)
      |                     ~^~~~~~~~~
match.cpp:66:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |         for(int j=0;j<v.size();j++)
      |                     ~^~~~~~~~~
match.cpp:71:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |                 for(int k=j+1;k<v.size();k++)
      |                               ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Runtime error 12 ms 19420 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Runtime error 12 ms 19420 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Runtime error 12 ms 19420 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -