Skip to content

Instantly share code, notes, and snippets.

@jason790228
Created May 23, 2017 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason790228/ed18450609884836ecda9fc1c5b0e395 to your computer and use it in GitHub Desktop.
Save jason790228/ed18450609884836ecda9fc1c5b0e395 to your computer and use it in GitHub Desktop.
551
// 551.cpp : 定義主控台應用程式的進入點。
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
#include <stack>
using namespace std;
enum MathSymbols
{
UNKNOWN,
PARENTHESES, // ( )
BRACKETS, // [ ]
BRACES, // { }
ANGLE_BRACKETS, // < >
PARENTHESE_STAR, // (* *)
MATH_SYMBOLS_SIZE
};
int ExpressionDetermine(std::string input)
{
const char *expression = input.c_str();
int expressionSize(input.length());
vector<int> countMathSymbols(MATH_SYMBOLS_SIZE, 0);
stack<MathSymbols> stackMathSymbols;
stackMathSymbols.push(UNKNOWN);
int countResult(0);
for (int i = 0; i < expressionSize; i++)
{
countResult++;
if (expression[i] == '(')
{
if ( expressionSize > i + 1
&& expression[i + 1] == '*')
{
stackMathSymbols.push(PARENTHESE_STAR);
countMathSymbols[PARENTHESE_STAR]++;
i++;
}
else
{
stackMathSymbols.push(PARENTHESES);
countMathSymbols[PARENTHESES]++;
}
}
else if (expression[i] == '[')
{
stackMathSymbols.push(BRACKETS);
countMathSymbols[BRACKETS]++;
}
else if (expression[i] == '{')
{
stackMathSymbols.push(BRACES);
countMathSymbols[BRACES]++;
}
else if (expression[i] == '<')
{
stackMathSymbols.push(ANGLE_BRACKETS);
countMathSymbols[ANGLE_BRACKETS]++;
}
else if (expression[i] == ')')
{
if (stackMathSymbols.top() == PARENTHESES && countMathSymbols[PARENTHESES] > 0)
{
countMathSymbols[PARENTHESES]--;
stackMathSymbols.pop();
}
else
{
return countResult;
}
}
else if (expression[i] == ']')
{
if (stackMathSymbols.top() == BRACKETS && countMathSymbols[BRACKETS] > 0)
{
countMathSymbols[BRACKETS]--;
stackMathSymbols.pop();
}
else
{
return countResult;
}
}
else if (expression[i] == '}')
{
if (stackMathSymbols.top() == BRACES && countMathSymbols[BRACES] > 0)
{
countMathSymbols[BRACES]--;
stackMathSymbols.pop();
}
else
{
return countResult;
}
}
else if (expression[i] == '>')
{
if (stackMathSymbols.top() == ANGLE_BRACKETS && countMathSymbols[ANGLE_BRACKETS] > 0)
{
countMathSymbols[ANGLE_BRACKETS]--;
stackMathSymbols.pop();
}
else
{
return countResult;
}
}
else if (expression[i] == '*' && (expressionSize > i + 1 && expression[i + 1] == ')'))
{
if (stackMathSymbols.top() == PARENTHESE_STAR && countMathSymbols[PARENTHESE_STAR] > 0)
{
countMathSymbols[PARENTHESE_STAR]--;
stackMathSymbols.pop();
i++;
}
else
{
return countResult;
}
}
}
for (int i = 0; i < MATH_SYMBOLS_SIZE; i++)
{
if (countMathSymbols[i] > 0)
{
return countResult + 1;
}
}
return 0;
}
int main()
{
vector<string> expressions;
vector<string> result;
string expression;
while (!cin.eof())
{
getline(cin, expression);
expressions.push_back(expression);
}
for (int i = 0; i < expressions.size()-1; i++)
{
int test;
test = ExpressionDetermine(expressions[i]);
if (test != 0)
{
cout << "NO " << test << endl;
}
else
{
cout << "YES" << endl;
}
}
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment