标签:style blog class c code java
// 度过了上周的悲催状态,我决定好好学习了……
//书上括号匹配是栈的简单应用,正好在SDOJ上看到这道题,顺便做了下
题目地址:SDOJ 2134
sin(20+10){[}]
yesno
Code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
int main()
{
char str[100];
int i,len,leaf;
stack<char> s;
while(gets(str))//用gets() 输入整行 可以包括空格 !!! 题目中字符串包含空格
{
len = strlen(str);
leaf = 1;
//printf("%d %s\n",len,str);
for(i = 0;i<len;i++)
{
if(str[i]==‘(‘||str[i]==‘{‘||str[i]==‘[‘)
s.push(str[i]);
if(str[i]==‘)‘)
{
if(!s.empty()&&s.top()==‘(‘)
s.pop();
else
{
leaf = 0;
break;
}
}
if(str[i]==‘]‘)
{
if(!s.empty()&&s.top()==‘[‘)
s.pop();
else
{
leaf = 0;
break;
}
}
if(str[i]==‘}‘)
{
if(!s.empty()&&s.top()==‘{‘)
s.pop();
else
{
leaf = 0;
break;
}
}
}
if(leaf&&s.empty())
printf("yes\n");
else
printf("no\n");
while(!s.empty())
s.pop();
}
return 0;
}
数据结构笔记之——括号匹配(栈的应用之一)(SDOJ 2134),布布扣,bubuko.com
数据结构笔记之——括号匹配(栈的应用之一)(SDOJ 2134)
标签:style blog class c code java
原文地址:http://blog.csdn.net/gray_1566/article/details/26269587