알고스팟 FIXPAREN
#include<iostream>
#include<string>
#include<stack>
#include<cstring>
using namespace std;
int main(){
char match[126], priority[126];
memset(match, 0, sizeof(match));
match['{'] = '}', match['('] = ')', match['<'] = '>', match['['] = ']';
string str, p;
stack<pair<char, int> > st;
int t; cin >> t;
while (t--){
cin >> str >> p;
for (int i = 0; i < p.size(); i++)
priority[match[p[i]]] = p.size() - i;
int cIdx = 0;
for (int i = 0; i < str.size(); i++){
if (match[str[i]] == 0){
if (priority[match[st.top().first]] >= priority[str[i]])
str[i] = match[st.top().first];
else
str[st.top().second] = str[i] + (str[i] == ')' ? -1 : -2);
st.pop();
}
else st.push(pair<char, int>(str[i], i));
}
cout << str << endl;
}
}
#include<string>
#include<stack>
#include<cstring>
using namespace std;
int main(){
char match[126], priority[126];
memset(match, 0, sizeof(match));
match['{'] = '}', match['('] = ')', match['<'] = '>', match['['] = ']';
string str, p;
stack<pair<char, int> > st;
int t; cin >> t;
while (t--){
cin >> str >> p;
for (int i = 0; i < p.size(); i++)
priority[match[p[i]]] = p.size() - i;
int cIdx = 0;
for (int i = 0; i < str.size(); i++){
if (match[str[i]] == 0){
if (priority[match[st.top().first]] >= priority[str[i]])
str[i] = match[st.top().first];
else
str[st.top().second] = str[i] + (str[i] == ')' ? -1 : -2);
st.pop();
}
else st.push(pair<char, int>(str[i], i));
}
cout << str << endl;
}
}
제가 본 코드 중 가장 좋은 코드라고 생각합니다. 도움 많이 됐습니다.
답글삭제감사합니다 ㅎㅎ
삭제if (priority[match[st.top().first]] >= priority[str[i]])
답글삭제str[i] = match[st.top().first];
else
str[st.top().second] = str[i] + (str[i] == ')' ? -1 : -2);
st.pop();
이쪽 조건 틀리신것 같습니다 ^^
오.. 잘못 된 소스를 올렸었네요. 지적 감사합니다.
답글삭제