C++アルゴリズムのかっこマッチング問題
1047 ワード
bool judge(const string& temp, stack& test)
{
int i =0;
char stack_pop_char;
while(i < temp.size() )
{
// , ,
if(temp[i] =='(' || temp[i] == '[' || temp[i] == '{')
{
test.push(temp[i]);
}
if(temp[i] == ')' || temp[i] == ']' || temp[i] == '}')
{
if(test.size() > 0) // , , False
{
stack_pop_char = test.top();
test.pop();
switch(temp[i])
{
case ')':
if (stack_pop_char == '('){
continue;
}
else return false;
case ']':
if (stack_pop_char == '['){
continue;
}
else return false;
case '}':
if (stack_pop_char == '{'){
continue;
}
else return false;
}
}
else return false;
}
++i;
}
return true;
}
括弧マッチングの問題は、文字列を左から右に遍歴すると、スタックの上部の左括弧が現在の位置に最も近い特性で動作します.スタックには、より有名なアプリケーション式の評価もあります.