답안 #1032505

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1032505 2024-07-23T21:44:33 Z MarwenElarbi 괄호 문자열 (CEOI16_match) C++17
100 / 100
10 ms 13408 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int nax=1e6+5;
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<char> ans(nax);
string t;
int dp[nax][26];
void rec(int l,int r){
    if(l>r) return;
    ans[l]='(';
    ans[dp[r][t[l]-'a']]=')';
    rec(l+1,dp[r][t[l]-'a']-1);
    rec(dp[r][t[l]-'a']+1,r);
}
int main()
{
    optimise;
    cin>>t;
    int n=t.size();
    reverse(t.begin(),t.end());
    t.pb(' ');
    reverse(t.begin(),t.end());
    stack<char> st;
    for (int i = 1; i <= n; ++i)
    {
        if(!st.empty()&&st.top()==t[i]) st.pop();
        else st.push(t[i]);
    }
    if(!st.empty()){
        cout <<-1<<endl;
        return 0;
    }
    for (int i = 1; i <= n; ++i)
    {
        dp[i][t[i]-'a']=i;
        if(i==1||dp[i-1][t[i]-'a']==0) continue;
        for (int j = 0; j < 26; ++j)
        {
            if(j!=(t[i]-'a')) dp[i][j]=dp[dp[i-1][t[i]-'a']-1][j];
        }
    }
    rec(1,n);
    for (int i = 1; i <= n; ++i)
    {
        cout <<ans[i];
    }cout <<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1368 KB Output is correct
2 Correct 0 ms 1372 KB Output is correct
3 Correct 1 ms 1336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1368 KB Output is correct
2 Correct 0 ms 1372 KB Output is correct
3 Correct 1 ms 1336 KB Output is correct
4 Correct 1 ms 1372 KB Output is correct
5 Correct 1 ms 1372 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1368 KB Output is correct
2 Correct 0 ms 1372 KB Output is correct
3 Correct 1 ms 1336 KB Output is correct
4 Correct 1 ms 1372 KB Output is correct
5 Correct 1 ms 1372 KB Output is correct
6 Correct 1 ms 1628 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 2140 KB Output is correct
9 Correct 1 ms 2140 KB Output is correct
10 Correct 1 ms 2140 KB Output is correct
11 Correct 1 ms 2140 KB Output is correct
12 Correct 5 ms 8636 KB Output is correct
13 Correct 6 ms 9308 KB Output is correct
14 Correct 7 ms 10272 KB Output is correct
15 Correct 8 ms 11864 KB Output is correct
16 Correct 8 ms 11868 KB Output is correct
17 Correct 9 ms 12380 KB Output is correct
18 Correct 10 ms 11612 KB Output is correct
19 Correct 10 ms 12632 KB Output is correct
20 Correct 6 ms 8792 KB Output is correct
21 Correct 9 ms 13408 KB Output is correct