답안 #589213

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
589213 2022-07-04T10:37:18 Z andrei_boaca 괄호 문자열 (CEOI16_match) C++14
100 / 100
115 ms 56132 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int baza=29,invbaza=758620695;
const int mod=1e9+7;
int n;
string s,sol;
vector<char> st;
vector<vector<int>> mypoz[2][27];
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;
    }
    int nr=1;
    ll h=0;
    nrm[h]=1;
    sum[0]=1;
    for(int i=1;i<=n;i++)
    {
        if(!st.empty()&&st.back()==s[i])
        {
            st.pop_back();
            h-=(s[i]-'a'+1);
            h=(1LL*h*invbaza)%mod;
        }
        else
        {
            h=((1LL*h*baza)+s[i]-'a'+1)%mod;
            st.push_back(s[i]);
        }
        if(nrm[h]==0)
        {
            nr++;
            nrm[h]=nr;
        }
        sum[i]=nrm[h];
    }
    for(int i=1;i<=26;i++)
        for(int j=0;j<=1;j++)
            mypoz[j][i].resize(nr+1);
    if(!st.empty())
    {
        cout<<-1;
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        if(!st.empty()&&st.back()==s[i])
        {
            st.pop_back();
            h-=(s[i]-'a'+1);
            h=(1LL*h*invbaza)%mod;
        }
        else
        {
            h=((1LL*h*baza)+s[i]-'a'+1)%mod;
            st.push_back(s[i]);
        }
        if(nrm[h]==0)
        {
            nr++;
            nrm[h]=nr;
        }
        sum[i]=nrm[h];
        mypoz[i%2][s[i]-'a'+1][sum[i]].push_back(i);
    }
    vector<int> last;
    sol.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        if(sol[i]!=')')
        {
            sol[i]='(';
            int maxpoz=n+1;
            if(!last.empty())
                maxpoz=last.back();
            int par=1-i%2;
            int lit=s[i]-'a'+1;
            int lft=0;
            int rgt=mypoz[par][lit][sum[i-1]].size();
            rgt--;
            int poz=0;
            while(lft<=rgt)
            {
                int mij=(lft+rgt)/2;
                if(mypoz[par][lit][sum[i-1]][mij]<maxpoz)
                {
                    poz=mypoz[par][lit][sum[i-1]][mij];
                    lft=mij+1;
                }
                else
                    rgt=mij-1;
            }
            sol[poz]=')';
            last.push_back(poz);
        }
        else
            last.pop_back();
    }
    for(int i=1;i<=n;i++)
        cout<<sol[i];
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 2 ms 720 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 1 ms 848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 2 ms 720 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 1 ms 848 KB Output is correct
8 Correct 4 ms 3028 KB Output is correct
9 Correct 6 ms 3736 KB Output is correct
10 Correct 8 ms 4572 KB Output is correct
11 Correct 8 ms 4692 KB Output is correct
12 Correct 87 ms 38976 KB Output is correct
13 Correct 86 ms 42580 KB Output is correct
14 Correct 76 ms 44460 KB Output is correct
15 Correct 11 ms 2008 KB Output is correct
16 Correct 13 ms 2000 KB Output is correct
17 Correct 65 ms 29124 KB Output is correct
18 Correct 15 ms 2128 KB Output is correct
19 Correct 103 ms 56132 KB Output is correct
20 Correct 71 ms 38936 KB Output is correct
21 Correct 115 ms 54604 KB Output is correct