(알고스팟) 괄호닫기.
bool wellmatched(const string& formula)
{
const string opening("({["), closing(")}]");
stack<char> openStack;
for (int i = 0; i < formula.size(); ++i)
{
if (opening.find(formula[i]) != -1)
openStack.push(formula[i]);
else{
if (openStack.empty())return false;
if (opening.find(openStack.top()) != closing.find(formula[i]))
return false;
}
}
return openStack.empty();
}
{
const string opening("({["), closing(")}]");
stack<char> openStack;
for (int i = 0; i < formula.size(); ++i)
{
if (opening.find(formula[i]) != -1)
openStack.push(formula[i]);
else{
if (openStack.empty())return false;
if (opening.find(openStack.top()) != closing.find(formula[i]))
return false;
}
}
return openStack.empty();
}
댓글
댓글 쓰기