UVA-673-栈-水题
2024-10-20 20:30:52
题意:
检测括号是否匹配,注意有空格
#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<math.h>
#include<sstream>
#include<queue>
#include<stack>
using namespace std;
struct Node
{
int r;
int c;
int total;
};
const Node dir[] = { { -1, 2 }, { 1, 2 }, { -2, 1 }, { 2, 1 }, { -2, -1 }, { 2,
-1 }, { -1, -2 }, { 1, -2 } }; int main()
{ string yes = "Yes";
string no = "No";
int n;
cin >> n;
string str;
getline(cin, str);
while (n--)
{
getline(cin, str); int length = str.length();
stack<char> s;
int ok = 1;
for (int i = 0; i < length; i++)
{
char c = str.at(i);
if (c == ' ')
continue;
if (c == '(' || c == '[')
{
s.push(c);
}
else if (c == ')' || c == ']')
{
if (s.size() == 0)
{
ok = 0;
break;
}
else
{
char cc = s.top();
s.pop();
if (c == ')')
{
if (cc != '(')
{
ok = 0;
break;
}
}
else
{
if (cc != '[')
{
ok = 0;
break;
}
}
}
}
}
if (s.size() != 0)
ok = 0; if (ok)
cout << yes << endl;
else
cout << no << endl;
}
}
最新文章
- 快速入门系列--WCF--08扩展与新特性
- javascript去掉空格
- 1069 Nim游戏
- Java——交通灯
- mysql特有语法
- JFrame 实现全屏透明背景
- 写自己的WPF样式 - 窗体
- nginx自定义模块编写-根据post参数路由到不同服务器
- meminfo,df,
- 百度api集合!
- NSIS 设置系统变量
- 浏览器各个版本和系统(chrome/safari/edge/qq/360)
- BZOJ1688|二进制枚举子集| 状态压缩DP
- Javascript - ExtJs - 常用方法和属性
- Give root password for maintenance(or type control -D to continue)
- delimiters 插值 选项
- Mybatis 系列2-配置文件
- Charles抓包https
- 基于Java的简易表达式解析工具(一)
- 第十一章 自己实现一致性hash算法