zoukankan      html  css  js  c++  java
  • NYOJ-括号配对问题 <技巧性的非栈道法>

    括号配对问题

    时间限制:3000 ms  |  内存限制:65535 KB

    难度:3

     描述
    现在,有一行括号序列,请你检查这行括号是否配对。
    输入
    第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符。
    输出
    每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No。
    样例输入
    3
    [(])
    (])
    ([[]()])
    样例输出
    No
    No
    Yes
    此题用栈道的话就去套那个思想模版就好,但是如果不用栈道的话。
    	只需要抓住一点那就是只要符合括号匹配的肯定有 () 或 [] 的情况,那么把这两个覆盖之后,肯定又会出现新的 () 或 [] ; 
    所以,上代码了

    奋斗

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 #include <fstream>
     5 #include <vector>
     6 
     7 #include <cstdio>
     8 #include <cstdlib>
     9 #include <cstring>
    10 #include <cmath>
    11 #include <climits>
    12 #include <cctype>
    13 using namespace std;
    14 typedef long long ll;
    15 
    16 int main() {
    17     int T;
    18     char a[10001];
    19     scanf("%d",&T);
    20     getchar();
    21     while(T--) {
    22         int i = 2;
    23         scanf("%c",&a[1]);
    24         while(scanf("%c", &a[i]) && a[i] != '
    ') {
    25             if ((a[i-1] == '[' && a[i] == ']') || (a[i-1] == '(' && a[i] == ')'))
    26                 i -= 1;
    27             else i++;
    28         } if (i == 1) puts("Yes");
    29         else puts("No");
    30     }
    31     return 0;
    32 }
    View Code

    欢迎码友一起讨论学习更简洁的算法。
    (由于我个人的习惯,喜欢把头文件都留着,这里只需要一个 cstdio 即可了,另外栈道的方法,我就不在这里贴了,因为博客上都快成灾了.)
  • 相关阅读:
    Uploadify v3.2.1 上传图片并预览
    mybatis批量操作
    500 拦截错误输出
    jsnop
    java目录
    设置360调用样式 IE调用样式
    Spring加载resource时classpath*:与classpath:的区别
    context:annotation-config 与context:component-scan
    Andriod调用http请求
    JDK环境变量
  • 原文地址:https://www.cnblogs.com/Ddlm2wxm/p/5931495.html
Copyright © 2011-2022 走看看