7-10 括号匹配(25 分)

给定一串字符,不超过100个字符,可能包括括号、数字、字母、标点符号、空格,编程检查这一串字符中的( ) ,[ ],{ }是否匹配。

输入格式:

输入在一行中给出一行字符串,不超过100个字符,可能包括括号、数字、字母、标点符号、空格。

输出格式:

如果括号配对,输出yes,否则输出no。

输入样例1:

sin(10+20)

输出样例1:

yes

输入样例2:

{[}]

输出样例2:

no

思路

用栈模拟 如果碰到是 左括号 就入栈

然后碰到 右括号 要判断 栈是不是空 如果空 就是不合法的

如果栈不空 那就要判断 栈顶 是不是对应的 左括号 如果不是 就不合法

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 2e2 + 5;
const int MOD = 1e9 + 7; int main()
{
string s;
getline(cin, s);
int len = s.size();
map <char, char> m;
m['('] = ')';
m['['] = ']';
m['{'] = '}';
stack <char> vis;
int flag = 1;
for (int i = 0; i < len; i++)
{
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
vis.push(s[i]);
else if (s[i] == ')' || s[i] == ']' || s[i] == '}')
{
if (vis.size() && m[vis.top()] == s[i])
vis.pop();
else
{
flag = 0;
break;
}
}
}
if (flag && vis.size() == 0)
printf("yes\n");
else
printf("no\n"); }

最新文章

  1. Spark源码学习1.5——BlockManager.scala
  2. the setting of serial port in the SecureCRT
  3. [VBS]脚本中的字典、动态数组、队列和堆栈
  4. Ubuntu 设置Vim tab为四个空格
  5. 首页banner特效
  6. Linq 用法笔记
  7. underscorejs-groupBy学习
  8. java之方法覆盖的坑
  9. win8如何显示文件后缀名
  10. centos永久修改主机名
  11. Linux用户深度管理
  12. 《HelloGitHub》第 18 期
  13. 一些精妙的sql语句收集
  14. what?iView的DropDown没有element的split-button?提issure?等不及了,自己实现一个
  15. jquery click嵌套 事件重复注册 多次执行的问题解决
  16. linux、centos下安装配置vim
  17. python中函数与函数式编程(二)
  18. 基于jQuery个性圆圈倒计时特效
  19. 机器学习结果加ID插入数据库源码
  20. centos7和Ubuntu上的关机需要手动关闭电源的问题

热门文章

  1. Unity -- 使用easyAR的基础教程
  2. VS2010中 报错:error C2146、error C4430 原因一:缺少CvvImage类
  3. C#网络编程:Socket编程
  4. linux 文件删除恢复extundelete
  5. Intellij IDEA 的使用
  6. 使用聚合数据的接口进行的RxAndroid学习
  7. crm操作业务部门
  8. UNP学习笔记(第三十章 客户/服务器程序设计范式)
  9. 使用struts2完成ckeditor和图片上传
  10. HDMI原理图信号PIN脚