`
jishublog
  • 浏览: 869490 次
文章分类
社区版块
存档分类
最新评论

UVa 673 - Parentheses Balance

 
阅读更多

UVa 673 - Parentheses Balance

Table of Contents

1题目

===================

Parentheses Balance

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

(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. Your program can assume that the maximum string length is 128.

Input

The file contains a positive integernand a sequence ofnstrings of parentheses()and[], one string a line.

Output

A sequence ofYesorNoon the output file.

Sample Input

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

Sample Output

Yes
No
Yes



Miguel Revilla
2000-08-14

===================

2思路

括号匹配问题,使用栈实现。需要注意的是除了检查括号是否匹配,还要检查是否完全匹配,即最后栈是否为空,这点容易出错。

3代码

/* 
 * Problem: UVa 673 - Parentheses Balance
 * Lang: ANSI C
 * Time: 0.029s
 * Author: minix
 */
#include <stdio.h>
#include <string.h>
#define N 128+10
char str[N];
char stack[N];
int top;
int main() {
  int n, i, j, len;
  scanf ("%d", &n); getchar();
  for (i=0; i<n; i++) {
    memset (str, 0, sizeof(str[0])*N);
    top = -1;
    fgets (str, sizeof(str[0])*N, stdin);
    len = strlen(str)-1;
    for (j=0; j<len; j++) {
      if (str[j]=='(' || str[j]=='[') stack[++top] = str[j];
      else {
        if (top>=0 && str[j]==')' && stack[top]=='(') 
          top--;
        else if (top>=0 && str[j]==']' && stack[top]=='[')
          top--;
        else
          break;
      }
    }
    if (j==len && top==-1) printf ("Yes\n");
    else printf ("No\n");
  }
  return 0;
}


分享到:
评论

相关推荐

    The-parentheses-matching-the-test.rar_Parentheses Matching_The T

    用C语言编写的,括号匹配的检验能够让人轻易的学会C语言和数据结构的使用。

    highlight-parentheses.el:移至SourceHut

    此项目已移至来源: ://sr.ht/~tsdh/highlight-parentheses.el/

    solarized-parentheses-lighttable-theme:带有日光化彩虹括号的光桌主题

    日圆括号 没有维护- lighttable 很漂亮,但它不是 vim。 主题,旨在与括号一起使用以使代码可读。 目前仅包括轻型变体。 需要彩虹插件的彩虹括号。 包含粗体和浅色变体的字体(例如 )使括号正确突出。

    2017湘潭大学邀请赛题目

    2017湘潭大学邀请赛题目

    gasstationleetcode-leetcode-rust:莱特代码休息

    加油站 leetcode 力码锈 问题 # 标题 命令 1 cargo run --bin 1-two-sum 2 cargo ...20-valid-parentheses 21 cargo run --bin 21-merge-two-sorted-lists 27 cargo run --bin 27-remove-element 28

    spacelite:Emacs的轻量级邪恶配置

    undo-tree flyspell用于拼写检查diminish清洁状态线magit与evil-magit 一些简单的MacOs绑定(仅在OSX / emacs中启用) 快捷键帮助的which-key solarized-dark主题highlight-parentheses , evil-anzu , restart-...

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    20-有效的括号:valid-parentheses 150-逆波兰表达式求值:evaluate-reverse-polish-notation 155-最小栈:min-stack Tree 普通二叉树 94-二叉树中序遍历:inorder-traversal 100-相同的二叉树:same-tree 101-对称二叉树...

    C++出错提示英汉对照表.txt

    Ambiguous operators need parentheses -----------不明确的运算需要用括号括起 Ambiguous symbol ''xxx'' ----------------不明确的符号 Argument list syntax error ----------------参数表语法错误 Array ...

    c语言程序调试中错误或警告提示的中文对照

    如:Ambiguous operators need parentheses -------不明确的运算需要用括号括起 Ambiguous symbol ''xxx'' ----------------不明确的符号 Argument list syntax error ----------------参数表语法错误 Array ...

    leetcode2-valid_parentheses:代码挑战:有效括号

    leetcode 2 有效括号 给定一个只包含字符'(' , ')' , '{' , '}' , '['和']'的字符串,确定输入字符串是否有效。 输入字符串在以下情况下有效: * 左括号必须由相同类型的括号封闭。...类别:堆栈、序列处理

    leetcode跳跃-leetcode:leetcode解题之路

    有效的括号](./Stack/valid-parentheses.md) Array 数组 [0004 寻找两个有序数组的中位数](./Array/README.md) [0045 跳跃游戏 II](./Array/jump-game-ii.md) [0053 最大子序和](./Array/maximum-subarray.md) [0041...

    LeetCode 括号生成

    题目来源:https://leetcode-cn.com/problems/generate-parentheses/submissions/ 题目 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。 例如,给出 n = 3,生成结果为...

    leetcode中国-LeetCodeAnswerCollections:LeetCode题解精选代码收集

    generate-parentheses【】【中等】 这是一道递归题目,想法很重要,保证括号合法性是另一个要点 private List result; public List generateParenthesis(int n) { result = new ArrayList(); _generate(0, 0, n, "")...

    C++出错提示英汉对照表

    Ambiguous operators need parentheses -----------不明确的运算需要用括号括起 Ambiguous symbol ''xxx'' ----------------不明确的符号 Argument list syntax error ----------------参数表语法错误 Array ...

    leetcode添加元素使和等于-leetcode:力码

    valid-parentheses 155 min-stack design-circular-deque trapping-rain-water 滑动窗口 通过窗口的大小不断调整来找到合适的值,或者窗口大小不变,通过左右移动来找到相应的结果 \ \ 其他 非 LeetCode 题,单纯在...

    SQL Prompt_9.1.8.4871破解版

    SP-6667 : Parentheses settings aren't working right for OUTER APPLY. SP-6807 : Maintains a space between close parentheses on RAISERROR and WITH clause. SP-6836 : Parentheses are not consistent in ...

    Parentheses Classifier-开源

    括号分类器采用一组括号的内容,并将其分类为几个类别之一。 它包括一个带括号的数据提取器和分类器。

    快速JSON序列化Easyjson.zip

    syntactically validated more than required to skip matching parentheses.No true streaming support for encoding/decoding. For many use-cases and protocols, data length is typically known on input and...

    leetcode答案-LeetCode:不要放弃

    leetcode 答案LeetCode 算法的复杂度分析。...因为是递回求解,所以很抽象不好懂,这里以leetcode的generate-parentheses一道题目为例,给定n,要找出n个括号的各种组合,例如n=2 ,答案会是[(()), ()()]

    parentheses:如何通过放置括号来给出最大数量

    parentheses:如何通过放置括号来给出最大数量

Global site tag (gtag.js) - Google Analytics