UVA - 673 Parentheses Balance
2016-07-21 20:52
567 查看
题目大意:匹配括号。空行也输出 Yes
注意这种:([(])) ——> No
解题思路:左括号入栈,右括号出栈,最终栈空 Yes
一开始想用计数偷懒,但是发现太天真了,([(]))这种情况无法解决= =
注意这种:([(])) ——> No
解题思路:左括号入栈,右括号出栈,最终栈空 Yes
一开始想用计数偷懒,但是发现太天真了,([(]))这种情况无法解决= =
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<ctype.h> using namespace std; char str[150]; char stack[150]; int main() { int n; scanf("%d", &n); getchar(); while (n--) { memset (stack, 0, sizeof(stack)); gets(str); int tag = 0; int len = strlen(str); for (int i = 0; i < len; i++) { if (str[i] == '(' || str[i] == '[') { stack[tag] = str[i]; tag++; } if (str[i] == ')') { if (stack[tag-1] == '(') { stack[tag-1] = '\0'; tag--; } else { stack[tag] = str[i]; tag++; } } if (str[i] == ']') { if (stack[tag-1] == '[') { stack[tag-1] = '\0'; tag--; } else { stack[tag] = str[i]; tag++; } } } if (stack[0] == '\0') printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- C++基于栈实现铁轨问题
- 浅析C语言中堆和栈的区别
- C语言栈的表示与实现实例详解
- C语言实现颠倒栈的方法
- 算法系列15天速成 第十天 栈
- 一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
- Array栈方法和队列方法的特点说明
- java数据结构之java实现栈
- 浅析栈区和堆区内存分配的区别
- 用Java代码实现栈数据结构的基本方法归纳
- 用PHP解决的一个栈的面试题
- 浅谈C#中堆和栈的区别(附上图解)
- C#使用Object类实现栈的方法详解
- JavaScript数据结构与算法之栈详解
- C语言之栈和堆(Stack && Heap)的优缺点及其使用区别
- Python实现栈的方法
- JavaScript数组的栈方法与队列方法详解
- JavaScript数据结构与算法之栈与队列
- JavaScipt中栈的实现方法
- Java中堆和栈的区别详解