# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
51389 | spencercompton | 괄호 문자열 (CEOI16_match) | C++17 | 2 ms | 492 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
cin >> str;
int n = (int)str.length();
vector<int> li[26];
for(int i = 0; i<n; i++){
li[str[i]-'a'].push_back(i);
}
bool ok = true;
for(int i = 0; i<26; i++){
ok &= li[i].size()%2==0;
}
if(!ok){
cout << -1 << endl;
return 0;
}
int bef[n];
bool left[n];
for(int i = 0; i<n; i++){
bef[i] = -1;
left[i] = false;
}
for(int i =0; i<26; i++){
for(int j = 0; j<li[i].size()/2; j++){
int ri = li[i].size()-1-j;
bef[li[i][ri]] = li[i][j];
left[li[i][j]] = true;
left[li[i][ri]] = false;
}
}
int ch = 0;
int last[n+2];
for(int i = 0; i<=n+1; i++){
last[i] = -1;
}
for(int i =0 ; i<n; i++){
if(left[i]){
last[ch++] = i;
}
else{
ch--;
if(ch<0){
ok = false;
break;
}
ok &= bef[i]==last[ch];
last[ch] =-1;
}
}
if(!ok){
cout << -1 << endl;
}
else{
string t = "";
for(int i= 0 ; i<n; i++){
if(left[i]){
t += "(";
}
else{
t += ")";
}
}
cout << t << endl;
}
}
컴파일 시 표준 에러 (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... |