uva673 Parentheses Balance

简介: uva673 Parentheses Balance
#include <stdio.h>#include <string.h>#define LOCALcharstr[130];
charstack[130];
intmain()
{
inti, j, n;
intlen;
inttop;
#ifndef LOCALfreopen("c://uva_in.txt", "r", stdin);
#endifscanf("%d", &n);
getchar();
for (i=0; i<n; i++)
    {
gets(str);
len=strlen(str);
if (len==0)
        {
printf("Yes/n");
continue;
        }
stack[0] =str[0];
top=0;
for (j=1; j<len; j++)
        {
if (str[j] =='('||str[j] =='[')
stack[++top] =str[j];
elseif (str[j] ==')')
            {
if (top>=0&&stack[top] =='(')
--top;
elsestack[++top] =str[j];
            } else            {
if (top>=0&&stack[top] =='[')
--top;
elsestack[++top] ==str[j];
            }
        }
if (top==-1)
printf("Yes/n");
elseprintf("No/n");
    }
return0;
}
目录
相关文章
|
7月前
codeforces 317 A Perfect Pair
我先排除了输出-1的,然后再考虑如何计算最小的步数。我们主要在每一步中最小一个加上另一个就可以了,这是朴素的求法,但可能出现这样的情况 比如 -100000000 1 10000000 这样的话会循环100000000多次,肯定超时,所以我们要加快速度。
23 0
|
canal
LeetCode 125. Valid Palindrome
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。
65 0
LeetCode 125. Valid Palindrome
LeetCode 241. Different Ways to Add Parentheses
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及 * 。
53 0
LeetCode 241. Different Ways to Add Parentheses
|
机器学习/深度学习
[LeetCode]--20. Valid Parentheses
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are a
1265 0
LeetCode - 20. Valid Parentheses
20. Valid Parentheses  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个括号序列,检查括号是否按顺序匹配.
847 0