답안 #239470

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
239470 2020-06-15T18:38:57 Z arvindr9 괄호 문자열 (CEOI16_match) C++14
0 / 100
6 ms 384 KB
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>

#include <iostream>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <iomanip>

// #include <bits/stdc++.h>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<double,double> pd;
typedef priority_queue<int, vector<int>, greater<int> > min_heap;

// template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const double PI = 4*atan(1);
const ll INF = 1e18;
const int MX = 100001;

string st;
int n;
const int maxn = 1e5 + 5;
vector<int> char_inds[27];
char ans[maxn];
set<int> right_inds;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    freopen("match.in", "r", stdin);
    freopen("match.out", "w", stdout);
    cin >> st;
    n = st.size();
    for (int i = 0; i < n; i++) {
        char_inds[st[i] - 'a'].push_back(i);
    }
    right_inds.insert(n);
    bool possible = true;
    for (int i = 0; i < n; i++) {
        if (right_inds.count(i)) {
            right_inds.erase(i);
            continue;
        }
        ans[i] = '(';
        int char_val = st[i] - 'a';
        int next_right_index = lb(char_inds[char_val].begin(), char_inds[char_val].end(), *(right_inds.ub(i))) - char_inds[char_val].begin() - 1;
        if (next_right_index < 0) {
            possible = false;
            break;
        }
        int char_ind = char_inds[char_val][next_right_index];
        if (char_ind > i) {
            right_inds.insert(char_ind);
            ans[char_ind] = ')';
        } else {
            possible = false;
            break;
        }
    }
    if (!possible) {
        cout << -1 << "\n";
    } else {
        for (int i = 0; i < n; i++) {
            cout << ans[i];
        }
        cout << "\n";
    }

}

Compilation message

match.cpp: In function 'int main()':
match.cpp:53:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("match.in", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:54:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("match.out", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -