zoukankan      html  css  js  c++  java
  • P1739 表达式括号匹配

    题目描述

    假设一个表达式有英文字母(小写)、运算符(+,—,*,/)和左右小(圆)括号构成,以“@”作为表达式的结束符。

    请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返回“YES”;否则返回“NO”。

    表达式长度小于255,左圆括号少于20个。

    输入格式

    一行:表达式

    输出格式

    一行:“YES” 或“NO”

    输入输出样例

    输入 #1

    2*(x+y)/(1-x)@
    

    输出 #1

    YES
    

    输入 #2

    (25+x)*(a*(a+b+b)@
    

    输出 #2

    NO
    

    说明/提示

    表达式长度小于255,左圆括号少于20个

    Code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00                 11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                  00.                                     ^^0.^
                                  ^ 0                                     ^^110.^
                              0   0 ^                                     ^^^10.01
                       ^^     10  1 1                                      ^^^1110.1
                       01     10  1.1                                      ^^^1111110
                       010    01  ^^                                        ^^^1111^1.^           ^^^
                       10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    // train —— 1739.cpp created by VB_KoKing on 2019-08-14.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
     
     Determination algorithm:
     
     Determining data structure:
     
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first gentle breeze that passed through my ear is you,
    The first star I see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    
    #include <stack>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <iostream>
    
    void solve();
    
    using namespace std;
    
    int main() {
        solve();
        return 0;
    }
    
    void solve() {
        string str;
        cin >> str;
        stack<bool> test;
    
        for (int i = 0; i < str.length(); i++) {
            if (str[i] == '(') test.push(true);
            else if (str[i] == ')') {
                if (test.empty()) {
                    printf("NO");
                    return;
                }
                test.pop();
            }
        }
    
        if (!test.empty()) {
            printf("NO");
            return;
        }
        printf("YES");
        return;
    }
    
  • 相关阅读:
    打开一个网页,以html代码保存于txt文件中
    用C查看系统任务管理器中运行的程序
    常见两种LINK错误
    怎么把下载的dll和def生成lib,以用于编程
    建立一个不能打开的文件(占坑)C语言高级API调用
    [转]软件版本命名格式
    回调函数编写和注重点
    ubuntu linux mysql 开发模式与连接编译
    创建一个进程和两个管道,实现与进程通信
    hdoj 1115 Lifting the Stone (求多边形重心)
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338106.html
Copyright © 2011-2022 走看看