제출 #874975

#제출 시각아이디문제언어결과실행 시간메모리
874975_fractal괄호 문자열 (CEOI16_match)C++14
100 / 100
6 ms12124 KiB
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define mp make_pair #define pb push_back #define pf push_front #define ppb pop_back #define ppf pop_front #define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define sz(x) (int)x.size() #define len(x) (int)strlen(x) #define all(x) x.begin(), x.end() #define debug cerr << "OK\n"; #define ub upper_bound #define lb lower_bound #define make_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end()) mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rofl(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef set<int> si; typedef set<ll> sll; typedef set<pii> spii; typedef set<pll> spll; typedef multiset <int> msi; typedef multiset <ll> msll; typedef map <int, int> mi; typedef map <ll, ll> mll; const int N = 1e5 + 2; const int M = 1e5; const int inf = 2e9 + 3; const ll INF = 1e18; const ld pi2 = 2.0 * 3.141592653589793; const ld pi = 3.141592653589793; const ld eps = 1e-12; const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; string s, t; int n; int dp[N][26]; void solve(int l, int r) { if (l > r) return; int ch = (s[l] - 'a'); if (dp[r][ch] <= l) return; int p = dp[r][ch]; t[l] = '('; t[p] = ')'; solve(l + 1, p - 1); solve(p + 1, r); } int main() { speed; cin >> s; n = sz(s); t = string(n, '0'); for (int i = 0; i < n; ++i) { for (int j = 0; j < 26; ++j) { dp[i][j] = -1; } int ch = (s[i] - 'a'); int p = (i > 0 ? dp[i - 1][ch] : -1); for (int j = 0; j < 26; ++j) { if (p > 0) { if (s[p - 1] - 'a' == j) dp[i][j] = max(dp[i][j], p - 1); else dp[i][j] = max(dp[i][j], dp[p - 1][j]); } } dp[i][ch] = i; } solve(0, n - 1); bool F = 1; for (auto c : t) if (c == '0') F = 0; if (!F) cout << -1 << '\n'; else cout << t << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...