SUDT 2134 - 数据结构实验之栈四:括号匹配
2014-04-05 17:56
337 查看
传送门SUDT 2134 - 数据结构实验之栈四:括号匹配
最简单的栈的应用.
记得一个月前看到这题时还不知道什么是栈, 用了很土的方法, 然后果断超时了. 之后去看解题报告, 根本不明白栈是什么意思, 觉得高端大气.
一个月..真快啊..
#include <cstdio>
#include <cstring>
using namespace std;
char stack[100];
int main()
{
//freopen("input.txt", "r", stdin);
char temp[100];
int i;
int top = -1; //一开始栈空
while (gets(temp) != NULL)
{
int flag = 0;
top = -1;
int len = strlen(temp);
for (i = 0; i < len; i++)
{
if (temp[i] == '{' || temp[i] == '(' || temp[i] == '[')
stack[++top] = temp[i];
else if (temp[i] == ')')
if (top == -1 || stack[top] != '(')
{
flag = 1;
break;
}
else
top--;
else if (temp[i] == '}')
if (top == -1 || stack[top] != '{')
{
flag = 1;
break;
}
else
top--;
else if (temp[i] == ']')
if (top == -1 || stack[top] != '[')
{
flag = 1;
break;
}
else
top--;
}
if (flag == 0 && top == -1)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
最简单的栈的应用.
记得一个月前看到这题时还不知道什么是栈, 用了很土的方法, 然后果断超时了. 之后去看解题报告, 根本不明白栈是什么意思, 觉得高端大气.
一个月..真快啊..
#include <cstdio>
#include <cstring>
using namespace std;
char stack[100];
int main()
{
//freopen("input.txt", "r", stdin);
char temp[100];
int i;
int top = -1; //一开始栈空
while (gets(temp) != NULL)
{
int flag = 0;
top = -1;
int len = strlen(temp);
for (i = 0; i < len; i++)
{
if (temp[i] == '{' || temp[i] == '(' || temp[i] == '[')
stack[++top] = temp[i];
else if (temp[i] == ')')
if (top == -1 || stack[top] != '(')
{
flag = 1;
break;
}
else
top--;
else if (temp[i] == '}')
if (top == -1 || stack[top] != '{')
{
flag = 1;
break;
}
else
top--;
else if (temp[i] == ']')
if (top == -1 || stack[top] != '[')
{
flag = 1;
break;
}
else
top--;
}
if (flag == 0 && top == -1)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
相关文章推荐
- 2134 数据结构实验之栈与队列四:括号匹配
- SDUT 2134 数据结构实验之栈与队列四:括号匹配
- sdut 2134 数据结构实验之栈与队列四:括号匹配
- SDUT 2134 数据结构实验之栈四:括号匹配
- 2134 数据结构实验之栈四:括号匹配
- 2134-数据结构实验之栈与队列四:括号匹配
- SDUT 2134 数据结构实验之栈四:括号匹配
- [SDUT](2134)数据结构实验之栈四:括号匹配 ---栈
- [2134]数据结构实验之栈四:括号匹配 sdutOJ
- [2134]数据结构实验之栈四:括号匹配 sdutOJ
- SDUT 2134 数据结构实验之栈四:括号匹配
- 数据结构实验之栈四:括号匹配
- sdut 数据结构实验之栈四:括号匹配
- 数据结构实验之栈四:括号匹配(有点麻烦了,凑合看吧)
- sdut 数据结构实验之栈四:括号匹配
- sdutacm-数据结构实验之栈四:括号匹配
- 数据结构实验之栈四:括号匹配
- 寒假第三天--栈和队列--数据结构实验之栈四:括号匹配 (数组)
- 寒假第三天--栈和队列--数据结构实验之栈四:括号匹配 (栈)
- SDUT OJ 数据结构实验之栈四:括号匹配