P1739 表达式括号匹配
生活随笔
收集整理的這篇文章主要介紹了
P1739 表达式括号匹配
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目描述
假設(shè)一個表達式有英文字母(小寫)、運算符(+,—,*,/)和左右小(圓)括號構(gòu)成,以“@”作為表達式的結(jié)束符。
請編寫一個程序檢查表達式中的左右圓括號是否匹配,若匹配,則返回“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^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^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.101 10 1.1 ^^^1111110010 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. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 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^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 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; }總結(jié)
以上是生活随笔為你收集整理的P1739 表达式括号匹配的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派 RespberryPi:通过命令
- 下一篇: P1305 新二叉树