#include<iostream>
#include <string>
#include <stack>
using namespace std;
bool isLeftSeq(char c);
bool isValidSeq(string input);
bool isCom(char c1, char c2);
int main()
{
string str = "{[{()[]}]}([])";
bool bl = isValidSeq(str);
cout << bl << endl;
return 0;
}
bool isValidSeq(string input)
{
unsigned count = 0;
stack<char> s;
for(string::iterator itr = input.begin(); itr != input.end(); itr++){
if(isLeftSeq(*itr))
s.push(*itr);
else {
if( !isCom(s.top(), *itr) )
count++;
s.pop();
}
}
return count == 0;
}
bool isLeftSeq(char c)
{
return (c == '(' || c == '{' || c == '[' );
}
bool isCom(char c1, char c2)
{
if( c1 == '{' )
return c2 == '}';
else if ( c1 == '[' )
return c2 == ']';
else
return c2 == ')';
}
括号配对
最新推荐文章于 2021-05-21 20:05:49 发布