# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1118026 | vjudge1 | 괄호 문자열 (CEOI16_match) | C++17 | 24 ms | 23640 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define SPEED ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define ALL(x) x.begin(), x.end()
#define intt long long
#define pb push_back
#define endl "\n"
const int sz = 3e6 + 5, L = 20;
intt dp[sz][27];
bool valid(string& s) {
stack<char>st;
for(int i = 0; i < s.size(); i++) {
if(!st.empty() && st.top() == s[i]) {
st.pop();
} else{
st.push(s[i]);
}
}
return st.empty();
}
string ans, s;
void rec(intt l, intt r) {
if(l < r) {
intt pos = dp[r][s[l] - 'a'];
ans[l - 1] = '(';
ans[pos - 1] = ')';
rec(l + 1, pos - 1);
rec(pos + 1, r);
}
}
void solve() {
cin >> s;
int n = s.size();
if(!valid(s)) {
cout << -1 << endl;
return;
}
ans.resize(n);
s = '?' + s;
bool ok = true;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < 26; j++) {
if(s[i] == char('a' + j)) {
dp[i][j] = i;
} else {
int last = dp[i-1][s[i]-'a'];
if(last > 0) {
dp[i][j] = dp[last-1][j];
}
}
}
}
rec(1, n);
cout << ans << endl;
}
int main() {
SPEED;
intt tst = 1;
// cin >> tst;
while (tst--) {
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |