swust oj 962
括号匹配问题
假设表达式中允许包含两种括号:圆括号和方括号。编写一个算法判断表达式中的括号是否正确配对。
输入
由括号构成的字符串,包含”(“、”)“、”[“和”]“。
输出
如果匹配输出YES,否则输出NO。
样例输入
[([][]())]
样例输出
YES 首先是手撕链栈的用法
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
typedef char Datetype;
using namespace std; typedef struct link{
Datetype date;
struct link *next;
}Lnode; void Initstack(Lnode *&L)
{
L=(Lnode *)malloc(sizeof(Lnode));
L->next=NULL;
} void destroystack(Lnode *&L)
{
Lnode *p=L,*r=p->next;
while(r!=NULL)
{
free(p);
p=r;
r=r->next;
}
free(p);
} void push(Lnode *L , Datetype e)
{
Lnode *p;
p = (Lnode *)malloc(sizeof(Lnode));
p->date = e ;
p->next=L->next;
L->next=p;
} bool stackempty(Lnode *L)
{
return(L->next==NULL);
} bool pop(Lnode *&L , Datetype &e)
{
Lnode *p;
if(L->next==NULL)
return false;
p=L->next;
L->next=p->next;
e=p->date;
free(p);
return true;
} bool gettop(Lnode *L, Datetype &e)
{
if(L->next==NULL)
return false;
e=L->next->date;
return true;
} int main()
{
char a[],x;
cin>>a;
Lnode *L=NULL;
Initstack(L);
for(int i=;i<strlen(a);i++)
{
if(stackempty(L)) //栈为空就直接入栈
{
push(L,a[i]);
}
else{
gettop(L,x);
if(x=='('&&a[i]==')') //配对后就出栈
{
pop(L,x);
}
else if(x=='['&&a[i]==']')
{
pop(L,x);
}
else
{
push(L,a[i]); //未配对成功就入栈
}
}
}
if(stackempty(L))
cout<<"YES";
else
cout<<"NO";
destroystack(L);
return ;
}
然后是利用STL的做法
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stack>
#include<string>
using namespace std;
char arr;
int main()
{
stack<char>st;
while(scanf("%c",&arr)&&arr!='\n')
{
if(st.empty())
{
st.push(arr);
continue;
}
if((arr==']'&&st.top()=='[')||(arr==')'&&st.top()=='('))
st.pop();
else
st.push(arr);
}
if(st.empty())
cout<<"YES";
else
cout<<"NO";
}
swust oj 962的更多相关文章
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 1026]--Egg pain's hzf
题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf ...
- [Swust OJ 1139]--Coin-row problem
题目链接: http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...
- [Swust OJ 385]--自动写诗
题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535 Descripti ...
随机推荐
- CDH下集成spark2.2.0与kafka(四十一):在spark+kafka流处理程序中抛出错误java.lang.NoSuchMethodError: org.apache.kafka.clients.consumer.KafkaConsumer.subscribe(Ljava/util/Collection;)V
错误信息 19/01/15 19:36:40 WARN consumer.ConsumerConfig: The configuration max.poll.records = 1 was supp ...
- How to copy the contents of std::vector to c-style static array,safely?
[问题] I am getting warning when using the std copy function. I have a byte array that I declare. byte ...
- Fiddler抓包_重定向AutoResponder
1. 文件及图片替换(Enable rules) 目的:允许从本地返回文件,代替服务器响应,而不用将文件发布到服务器[可用正式环境验证本地文件] 步骤一:抓页面http://ir.baidu.com/ ...
- 【C++】C++中const与constexpr的比较
先说结论相同点:const和consexpr都是用来定义常量的.不同点:const声明的常量,初始值引用的对象不一定是一个常量:constexpr声明的常量,初始值一定是常量表达式. constexp ...
- Android Glide 源码分析系列(待完成)
参考:https://jekton.github.io/2018/06/08/glide-disk-cache/ 参考:https://jekton.github.io/2018/06/20/glid ...
- Atitit s2018 s3 doc list alldvc.docx .docx s2018 s3f doc compc s2018 s3f doc homepc sum doc dvcCompc dtS312 s2018 s3f doc compc\Atitit PathUtil 工具新特性新版本 v8 s312.docx s2018 s3f doc compc\Atitit 操作日
Atitit s2018 s3 doc list alldvc.docx .docx s2018 s3f doc compc s2018 s3f doc homepc sum doc dvcCompc ...
- [lvs]lvs的三种模式
回顾了下lvs的三种模式的调度机制 1.lvs的dr模式中的arp的抑制,eth用自己口arp回应. 2.keepalive是否直接操作rs? 不直接操作, 只操作dr(配lvs) 3.tunnel模 ...
- layer 关闭当前弹出层
关闭当前框的弹出层 layer.close(layer.index); 刷新父层 parent.location.reload(); // 父页面刷新 关闭iframe 弹出的全屏层 var inde ...
- 什么是面向切面编程AOP--知识点汇总
最近在学这方面的内容,读到的这段话我感觉说的很清楚了:这种在运行时,动态地将代码切入到类的指定方法.指定位置上的编程思想就是面向切面的编程. 面向切面编程(AOP是Aspect Orie ...
- Python2.x与3.x版本区别
Python2.x与3.x版本区别 1.print 函数 print语句没有了,取而代之的是print()函数. Python 2.6与Python 2.7部分地支持这种形式的print语法.在Pyt ...