博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA673 Parentheses Balance【堆栈+输入流】
阅读量:7011 次
发布时间:2019-06-28

本文共 1360 字,大约阅读时间需要 4 分钟。

  You are given a string consisting of parentheses () and []. A string of this type is said to be correct:

(a) if it is the empty string

(b) if A and B are correct, AB is correct,

(c) if A is correct, (A) and [A] is correct.

  Write a program that takes a sequence of strings of this type and check their correctness. Yourprogram can assume that the maximum string length is 128.InputThe file contains a positive integer n and a sequence of n strings of parentheses ‘()’ and ‘[]’, one stringa line.OutputA sequence of ‘Yes’ or ‘No’ on the output file.

Sample Input

3

([])

(([()])))

([()[]()])()

Sample Output

Yes

No

Yes

问题链接

问题简述参见上文。

问题分析这个是一个括号匹配问题,自然需要用堆栈

程序说明

输入方面用字符流是最佳的方案。

堆栈就没有必要用STL的堆栈(时间上慢),自己简单用数组实现一个堆栈是轻而易举的事情。

题记:(略)

参考链接:(略)

AC的C语言程序如下:

/* UVA673 Parentheses Balance */#include 
#define N 128char stack[N];int ps;int main(void){ int n; char c; scanf("%d", &n); getchar(); while(n--) { ps = 0; while((c = getchar()) != '\n') { if(ps > 0) { if((stack[ps - 1] == '(' && c == ')') || (stack[ps - 1] == '[' && c == ']')) ps--; else stack[ps++] = c; } else stack[ps++] = c; } printf("%s\n", (ps > 0) ? "No" : "Yes"); } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7563572.html

你可能感兴趣的文章
Java compiler level does not match解决方法(转)
查看>>
ROS初级教程 cmake cmakelist.txt 的编写教程
查看>>
Comparing Inline and Multi-Statement Table valued UDFs
查看>>
python 机器学习
查看>>
php如何控制客户端生成缓存
查看>>
不错的在线印章生成器网站
查看>>
Arduino控制LCD显示helloworld
查看>>
线程、任务和同步学习笔记(一)
查看>>
JavaScript this
查看>>
OpenJudge/Poj 1163 The Triangle
查看>>
POJ 3130 半平面交+模版改进
查看>>
Python基础二
查看>>
AndroidStudio -- AndroidStuido中找不到cache.properties文件
查看>>
nginx 无法访问root权限的文件内容
查看>>
进程和线程有什么区别?
查看>>
关于text-align无法居中的问题
查看>>
Here We Go(relians) Again
查看>>
Signal函数
查看>>
[Errno 14] PYCURL ERROR 7 - "couldn't connect to host"
查看>>
chrono
查看>>