답안 #589166

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589166 2022-07-04T09:50:08 Z andrei_boaca 괄호 문자열 (CEOI16_match) C++14
0 / 100
6 ms 9684 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+5);
    for(int i=1;i<=n;i++)
        sol[i]='a';
    for(int i=1;i<=26;i++)
    {
        v=mypoz[i];
        int suma=0;
        nrm.clear();
        int nr=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:49:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |         for(int j=0;j<v.size();j++)
      |                     ~^~~~~~~~~
match.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |         for(int j=0;j<v.size();j++)
      |                     ~^~~~~~~~~
match.cpp:72:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |                 for(int k=j+1;k<v.size();k++)
      |                               ~^~~~~~~~~
match.cpp:46:13: warning: variable 'nr' set but not used [-Wunused-but-set-variable]
   46 |         int nr=0;
      |             ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 6 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 6 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 6 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -