# Use spark, ANSJ segmentation to perform frequent statistics

2023-01-30

### 1. Inspection of bracket matching

Assuming that the expression allows three brackets: parentheses, square brackets, and brackets, the order of nested, that is, {() []} or [()}], etc. Success, [(]) or (()}, etc. are incorrect formats, that is, not matching. Test whether the parentheses match the method of “expectation of the urgency” to be described.

parentheses matching is divided into the following situations:

``````char a[] = "(())abc{[(])}" ;// The sequence of the brackets is incorrect
char b[] = "(()))abc{[]}" ;// Right brackets are more than left brackets
char c[] = "(()()abc{[]}" ;// Left brackets are more than right brackets
char d[] = "(())abc{[]()}" ;// Boolithelon matching is correct``````

### 2. Code implementation

code is as follows:

``````#include <iostream>
#include <stack>
using namespace std;

bool MatchBrackets(char str[], size_t size)
{
stack<char> s;
for (size_t i = 0; i < size; ++i)
{
if (str[i] != '(' && str[i] != ')' &&
str[i] != '[' && str[i] != ']' &&
str[i] != '{' && str[i] != '}')
{
continue;
}
else
{
if ('(' == str[i] || '{' == str[i] || '[' == str[i])
{
s.push(str[i]);
}
else
{
if (s.empty())
{
cout << "Right brackets are more than left brackets" << endl;
return false;
}
char c = s.top();
if (('(' == c && ')' == str[i]) ||
('{' == c && '}' == str[i]) ||
('[' == c && ']' == str[i]))
{
s.pop();
}
else
{
cout << "parentheses order does not match" << endl;
return false;
}
}
}
}
if (!s.empty())
{
cout << "There are more brackets than right brackets" << endl;
return false;
}
cout << "Correct match" << endl;
return true;
}

int main()
{
char a[] = "(())abc{[(])}";  // The sequence of the brackets is incorrect
char b[] = "(()))abc{[]}";   // Right brackets are more than left brackets
char c[] = "(()()abc{[]}";   // Left brackets are more than the right serum
char d[] = "(())abc{[]()}";  // The side parentheses match correctly

cout << MatchBrackets(a, strlen(a)) << endl;
cout << MatchBrackets(b, strlen(b)) << endl;
cout << MatchBrackets(c, strlen(c)) << endl;
cout << MatchBrackets(d, strlen(d)) << endl;
return 0;
}``````

source