Submission #476728

#TimeUsernameProblemLanguageResultExecution timeMemory
476728wiktoria_bazan괄호 문자열 (CEOI16_match)C++14
10 / 100
1 ms332 KiB
#include <iostream> #include <stack> #include <algorithm> #include <string> #include <vector> #include <queue> using namespace std; int const N = 1e5 + 9; int n; string s, w; stack <char> S; int ile[N][30]; int nt[30]; vector<int> V[30]; priority_queue<int> Q; int check(int in, int end) { int a = s[in] - 'a'; for (int i = V[a].size() - 1; i >= 0; i--) { int nIn = V[a][i]; if (nIn <= in) return -1; if (w[nIn] == ')') continue; if (nIn > end) continue; bool t = true; for (int j = 0; j < 30; j++) { nt[j] = ile[nIn][j]; if(in > 0) nt[j] -= ile[in - 1][j]; if (nt[j] % 2 == 1) t = false; } if (t == true) return nIn; } return -1; } void task() { w = ""; for (int i = 0; i < n; i++) { V[s[i] - 'a'].push_back(i); if (i > 0) { for (int j = 0; j < 30; j++) ile[i][j] = ile[i - 1][j]; } ile[i][s[i] - 'a']++; w.insert(w.size(), "0"); } for (int i = 0; i < n; i++) { if (w[i] == ')') continue; w[i] = '('; int e; if (Q.empty()) e = n; else { e = -Q.top(); while (e < i) { Q.pop(); if (Q.empty()) { e = n; break; } e = -Q.top(); } } int ch = check(i, e); if (ch == -1) { cout << -1; return; } w[ch] = ')'; Q.push(-ch); } cout << w; return; } int main() { cin >> s; n = s.size(); task(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...