题目描述
假设一个表达式由英文字母(大小写)数字00到99、运算符(+,-,*,/)和左右小括号(),左右中括号[]和左右大括号{}构成,以“@”作为表达式的结束符。请编写一个程序检查表达式中的左右括号是否匹配,若匹配,则输出“YES”;否则输出“NO”。表达式长度不超过107107。
输入格式
一行:表达式
输出格式
一行:“YES” 或“NO”
样例输入
fs{f}fsd[(f457jkl)fsdaf]@
样例输出
YES
样例输入
fs{{f}fsd[(fsadfjkl)fsdaf]@
样例输出
NO
样例输入
)@
样例输出
NO
样例输入
([)]@
样例输出
NO
参考代码
#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
using namespace std;
int main()
{
stack <char> s;
int i;
//scanf("%s", &str);
char c;
while(scanf("%c", &c))
{
//printf("%c", str[i]);
if(c == '@')
break;
if(c == '(' || c == '[' || c == '{')
s.push(c);
if(c == ')')
{
if(s.empty())
{
printf("NO");
return 0;
}
if(s.top() == '(')
s.pop();
else
{
printf("NO");
return 0;
}
}
if(c == ']')
{
if(s.empty())
{
printf("NO");
return 0;
}
if(s.top() == '[')
s.pop();
else
{
printf("NO");
return 0;
}
}
if(c == '}')
{
if(s.empty())
{
printf("NO");
return 0;
}
if(s.top() == '{')
s.pop();
else
{
printf("NO");
return 0;
}
}
}
if(s.empty())
printf("YES");
else
printf("NO");
return 0;
}