• 栈 ( ) [ ] 符号匹配问题


     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;
    }
    }

  • 相关阅读:
    第二次结对作业
    软件工程第一次结对作业2
    软件工程第一次结对作业1<embed border="0" marginwidth="0" marginheight="0" width=330 height=86 src="/music.163.com/outchain/player?type=0&id=273848636&auto=1&height=66"></embed>
    第三次软件工程作业——两题
    第三次软件工程作业——最大连续子数组和(最大子段和)
    第三次软件工程作业——商场营销税额
    软件工程第二次作业
    软件工程第一次作业
    Markdown 使用说明(转CSDN)
    大坑!常被忽视又不得不注意的小细节——%I64,%lld与cout(转载)
  • 原文地址:https://www.cnblogs.com/carry-2017/p/7210261.html
Copyright © 2020-2023  润新知