zoukankan      html  css  js  c++  java
  • 数据结构实验之栈四:括号匹配

                                                                                        数据结构实验之栈四:括号匹配

    Description

      给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。

    Input

     输入数据有多组,处理到文件结束。

    Output

      如果匹配就输出“yes”,不匹配输出“no”

    Sample Input

    sin(20+10)
    {[}]

    Sample Output

    yes
    no

    Hint

     
    #include<stdio.h>
    #include<string.h>
    char a[51];
    int main(){
      char test[51];
      while(gets(test)){
         int len=strlen(test), ans=0, flag=0;
         for(int i=0; i<len; i++){
            if(test[i]=='[' || test[i]=='(' || test[i]=='{')
               {  a[ans++] = test[i];  flag = 1; }
            else if( ( test[i]==']'&&a[ans-1]=='[' ) || ( test[i]=='}'&&a[ans-1]=='{' ) || ( test[i]==')'&&a[ans-1]=='(' ) )
                ans--;
            else if( ( test[i]==']'&&a[ans-1]!='[' ) || ( test[i]=='}'&&a[ans-1]!='{' ) || ( test[i]==')'&&a[ans-1]!='(' ) || ( (a[ans-1]==']' || a[ans-1]=='}' || a[ans-1]==')') && ans==0 ) )
                { flag=0; break; }
            }
         if(!ans && flag) printf("yes
    ");
         else  printf("no
    ");
      }
      return 0;
    }
    


    
    
  • 相关阅读:
    pugixml
    C++ 头文件的理解
    图像的特征
    光圈与景深
    Unix高级环境编程
    用libtommath实现RSA算法
    【linux+C】神器 vim + 指针相关客串
    【算法25】对称子字符串的最大长度
    设计并实现同时支持多种视频格式的流媒体点播系统
    递归再一次让哥震惊了
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/9079905.html
Copyright © 2011-2022 走看看