Submission #341627

#TimeUsernameProblemLanguageResultExecution timeMemory
341627phathnv괄호 문자열 (CEOI16_match)C++11
0 / 100
1 ms364 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5 + 1; const int AL = 26; int n, match[N], pre[N][AL], answer[N]; string s; void readInput(){ cin >> s; n = s.size(); s = '*' + s; } void process(int l, int r){ if (l > r) return; int best = pre[r][s[l] - 'a']; if (match[l] == best){ answer[l] = 1; answer[best] = 2; process(l + 1, best - 1); } else { int a = match[l]; int b = match[best]; answer[l] = 1; answer[match[l]] = 1; answer[match[best]] = 2; answer[best] = 2; process(l + 1, match[l] - 1); process(match[l] + 1, match[best] - 1); process(match[best] + 1, best - 1); } process(best + 1, r); } void solve(){ stack <int> st; for(int i = 1; i <= n; i++){ if (st.empty()){ st.push(i); } else if (s[st.top()] == s[i]){ match[st.top()] = i; match[i] = st.top(); st.pop(); for(int j = 0; j < AL; j++) pre[i][j] = pre[match[i] - 1][j]; pre[i][s[i] - 'a'] = i; } else { st.push(i); } } if (!st.empty()){ cout << -1; return; } process(1, n); for(int i = 1; i <= n; i++) cout << (answer[i] == 1? '(' : ')'); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); readInput(); solve(); return 0; }

Compilation message (stderr)

match.cpp: In function 'void process(int, int)':
match.cpp:28:13: warning: unused variable 'a' [-Wunused-variable]
   28 |         int a = match[l];
      |             ^
match.cpp:29:13: warning: unused variable 'b' [-Wunused-variable]
   29 |         int b = match[best];
      |             ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...