zoukankan      html  css  js  c++  java
  • 栈 ( ) [ ] 符号匹配问题

     E - Parentheses Balance

    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. Your program can assume that the maximum string length is 128. Input The file contains a positive integer n and a sequence of n strings of parentheses ‘()’ and ‘[]’, one string a line. Output A sequence of ‘Yes’ or ‘No’ on the output file. Sample Input 3 ([]) (([()]))) ([()[]()])() Sample Output Yes No Yes

    错误代码1!

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    int main()
    {
    int t,sum1,sum2;
    cin>>t;   //没有考虑不输入任何字符的情况 。当不输入任何字符时,系统要求通过,但此程序却无法通过,因为不输入并没有被考虑,cin也无法识别
    for(int i=1;i<=t;i++)
    {
    char ch[128];
    cin>>ch;
    int leng=strlen(ch);
    int sum1=100,sum2=100;
    if((leng+2)%2==0)
    {
    for(int j=0;j<leng;j++)
    {
    if(ch[j]==' ') break;
    if(ch[j]=='(')sum1++;
    if(ch[j]==')')sum1--;
    if(ch[j]=='[')sum2++;
    if(ch[j]==']')sum2--;
    }
    if(sum1==100&&sum2==100)
    cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    }
    else cout<<"No"<<endl;
    }
    }

    错误代码2!

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    int main()
    {
    int t,sum1,sum2;
    cin>>t;
    getchar();
    for(int i=1;i<=t;i++)
    {
    char ch[200];
    gets(ch);
    int leng=strlen(ch);
    int sum1=100,sum2=100;
    if(leng==0)cout<<"Yes"<<endl; / /更正了不输入任何字符时输出Yes的情况考虑
    else{
    if((leng+2)%2==0)
    {
    for(int j=0;j<leng;j++)
    {
    if(ch[j]=='(')sum1++;
    if(ch[j]==')')sum1--;
    if(ch[j]=='[')sum2++;
    if(ch[j]==']')sum2--;
    }
    if(sum1==100&&sum2==100) // 仅仅是数量上的考虑,忽略了题目对“格式”的要求
    cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    }
    else cout<<"No"<<endl;
    }
    }
    }

    //许多人做了半天题,却发现题目意思完全没有理解

    下面解释一下题目要求:

    1. (  ) [ ] 的分别个数要相同

    2. (  ) [  ]要匹配

    例如:(【)】  :不通过。将()记做A,[   ] 记做B,原表达式的()中既不为A,也不为B,不符合题目要求

    3.当不输入任何括号时,将输出Yes。

    所以从第二点要求来看此题,我们难以再用数组的方法来做此题目

    我们选用一种更为简单的方法 :

    一个超时的程序!

    #include<iostream>
    #include<stack>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    int main()
    {
    int t,i;
    char ch[130];
    cin>>t;
    getchar();
    while(t--)
    {
    stack<int>st;
    gets(ch);
    int leng=strlen(ch);
    for(i=0;i<leng;i++)
    {
    if(ch[i]=='('){st.push(1);continue;}
    if(ch[i]=='['){st.push(2);continue;}
    if((ch[i]==')'&&st.top()==1)||(ch[i]==']'&&st.top()==2))st.pop(); else break;
    }
    if(st.empty()) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    }
    }

  • 相关阅读:
    编程范式 epesode7,8 stack存放指针类型and heap,register
    编程范式 episode 6 实现stack 栈功能_ to do
    C 运算符优先级
    编程范式 episode3 and 4,5
    编程范式 epesode2 negative values, float 精度
    C 数据类型 长度
    memcpy code
    排序算法 2 qsort 库函数,泛型函数
    sin, miss the mark, correct our aim and try again
    排序算法 1
  • 原文地址:https://www.cnblogs.com/carry-2017/p/7210261.html
Copyright © 2011-2022 走看看