답안 #589090

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589090 2022-07-04T09:15:49 Z andrei_boaca 괄호 문자열 (CEOI16_match) C++14
0 / 100
10 ms 9940 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> mysums[100005];
map<int,int> nrm;
int nxt[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;
            }
            mysums[nrm[suma]].insert(v[j]);
        }
        suma=0;
        for(int j=0;j<v.size();j++)
        {
            if(sol[v[j]]!=')')
            {
                sol[v[j]]='(';
                auto it=prev(mysums[nrm[suma]].end());
                int poz=(*it);
                sol[poz]=')';
                mysums[nrm[suma]].erase(it);
            }
            if(v[j]%2==1)
                suma++;
            else
                suma--;
        }
        for(int j=1;j<=nr;j++)
            mysums[j].clear();
    }
    for(int i=1;i<=n;i++)
        cout<<sol[i];
    return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:47:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |         for(int j=0;j<v.size();j++)
      |                     ~^~~~~~~~~
match.cpp:61:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |         for(int j=0;j<v.size();j++)
      |                     ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Runtime error 10 ms 9940 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Runtime error 10 ms 9940 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Runtime error 10 ms 9940 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -