제출 #51389

#제출 시각아이디문제언어결과실행 시간메모리
51389spencercompton괄호 문자열 (CEOI16_match)C++17
0 / 100
2 ms492 KiB
#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) 메시지

match.cpp: In function 'int main()':
match.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0; j<li[i].size()/2; j++){
                  ~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...