题意:看括号是否匹配

解题思路:一直想用递归来做,发现做不出  后来才想到消去当今可以消去的() 与 【】 的括号  循环找就行了

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char str[1000];
int main()
{int t ;scanf("%d",&t);getchar();while(t--){memset(str,0,sizeof(str));gets(str);int ok = 0;int k = strlen(str);int sum = 1;while(sum != 0){sum = 0; for(int i = 0 ;i < k;i ++){if(str[i] == '('){for(int j = i+1 ;j < k ; ){if(str[j] == ')'){str[i] = '0';str[j] = '0';sum ++;break;}else if(str[j] == '0')j++;elsebreak;}}if(str[i] == '['){for(int j = i+1 ;j < k ; ){if(str[j] == ']'){str[i] = '0';str[j] = '0';sum ++;break;}else if(str[j] == '0')j++;elsebreak;}}}//puts(str);
        }for(int i = 0 ;i < k ;i ++)if(str[i] != '0')ok = 1;if(ok == 1)printf("No\n");elseprintf("Yes\n");}return 0;
}

View Code

解题思路2:用栈的思路! 比较当前和栈顶元素

// File Name: uva6731.c
// Author: darkdream
// Created Time: 2013年05月15日 星期三 20时24分53秒

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
char str[1000];
int main(){//freopen("/home/plac/problem/input.txt","r",stdin);//freopen("/home/plac/problem/output.txt","w",stdout);int a[1000];int b[1000];int t ;scanf("%d",&t);getchar();while(t--){gets(str);memset(a,0,sizeof(a));memset(b,0,sizeof(b));for(int i = 0 ;i < strlen(str); i++){if(str[i] == '(') b[i] = 1;else if(str[i] == ')')b[i] = -1;else if(str[i] == '[')b[i] = 2;else if(str[i] == ']')b[i] = -2;    }int j = 0 ;for(int i = 0; i < strlen(str);i++){if(b[i] != 0){if(b[i] + a[j] == 0 && b[i] < 0){      a[j] = 0;j--;}else{j++;a[j] = b[i];}}}if(a[1] == 0)printf("Yes\n");else printf("No\n");}return 0 ;
}

View Code

转载于:https://www.cnblogs.com/zyue/archive/2013/05/15/3080661.html

uva673 Parentheses Balance相关推荐

  1. UVa673 - Parentheses Balance

    //UVa673 - Parentheses Balance //已AC #include<iostream> #include<string> #include<sta ...

  2. UVA-673:Parentheses Balance踩坑

    我傻了我 基本思路就是一个stack的出入栈,没啥好说.主要是"empty string"这个条件把我搞到了. 因为要判断空字符串,所以我在string s; cin>> ...

  3. Parentheses Balance UVA - 673

    Parentheses Balance UVA - 673 在网上看了很多题解,没看到用map做的(可能是我没找到吧).其实用map可以少用很多if语句! #include<cstdio> ...

  4. UVA 673-Parentheses Balance

    UVA 673-Parentheses Balance 题目大意:1 A =[]或()算正确 2 A为BC(B,C都是正确的话)算正确 3 A = [M]或(M)(M为正确的话)算正确 解题思路:用栈 ...

  5. 【uva-673】 Parentheses Balance(括号匹配问题)

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

  6. 【紫书】UVA673 平衡的括号 Parentheses Balance

    题目提交点 UVA673 思路: 运用栈的思想,遇到'(' 和 ']' 入栈,遇到'('.'['出栈. 代码: #include<iostream> #include<stack&g ...

  7. 习题6-1 UVA673 平衡的括号 Parentheses Balance

    难度:2 一看难度是2知道不是很难,用栈比较方便,然后一个需要注意的地方就是,空也为正确,那么输入就可能为空,所以输入的时候要读入一整行. #include <bits/stdc++.h> ...

  8. uva 673 Parentheses Balance

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

  9. Parentheses Balance (括号平衡)---栈

    题目链接:https://vjudge.net/contest/171027#problem/E Yes的输出条件: 1. 空字符串 2.形如()[]; 3.形如([])或者[()] 分析: 1.设置 ...

最新文章

  1. Python设计模式-中介者模式
  2. hbase java api 两种方式
  3. Primes on Interval
  4. 【java基础】——java枚举类型基本介绍
  5. 学习Spring Boot:(十八)Spring Boot 中session共享
  6. SAP License:数据导入时的问题与总结
  7. python 线程安全的数据类型_详解python多线程、锁、event事件机制的简单使用
  8. win10有源信号分辨率怎么调_示波器高灵敏度小信号测量的注意事项
  9. linux logo程序设计,Android开机LOGO的修改
  10. 日记2021/9/28
  11. JS(fullpage)的使用
  12. 【VMware】【XShell】从虚拟机下载文件到本地
  13. bot机器人账号八大类特征分析---识别问题账号
  14. 机器学习基础---pr曲线的绘制
  15. a标签rel=“external nofollow“ 有什么作用和external
  16. 好用合并工具安利!需要的来关注一下
  17. gym/101955/problem/E - The Kouga Ninja Scrolls 线段数 维护 切比雪夫距离 2018沈阳icpc
  18. 致远SPM解决方案之沟通管理
  19. android-adapterservice
  20. 爬取东方财富网的部分股票信息(2)

热门文章

  1. 网络营销——网络营销专员浅析不同企业之间的营销推广各有差异
  2. php 静态方法继承,php 5.2中的类继承:重写扩展类中的静态变量?
  3. 写出python中的六种数据类型_python 数据类型1
  4. 量子计算机物理学,百年的超越:量子物理学与量子计算机
  5. android 数字时钟代码大全,Android自定义view实现数字时钟
  6. Powershell 渗透测试工具-Nishang
  7. 机器学习 数据量不足问题----1 做好特征工程 2 不要用太多的特征 3 做好交叉验证 使用线性svm...
  8. mysql Navcat触发器生成订单号
  9. SpringBoot Actuator监控【转】
  10. python四个带 key 参数的函数(max、min、map、filter)