<> Title Description

Please write a program , Determine whether the brackets in the given expression match , The legal bracket in the expression is ”(“, “)”, “[", "]“, “{“,
”}”, The three brackets can be nested in any order .

<> input

There are multiple expressions , The first line of input data is the number of expressions , Each expression occupies one line .

<> output

For each expression , If the brackets match , Then output ”yes”, Otherwise output ”no”.

<> sample input
4 [(d+f)*{}] [(2+3)) ()} [4(6]7)9
<> sample output
yes no no no
thinking : use stack as well as map, adopt map take ”(“, “)”, “[", "]“, “{“,
”}” Any two of them are tied together . After entering a string , If the elements of the string are parentheses , Put it on the stack , If the bracket is a pair with the top element of the stack , The top of the stack element is removed from the stack . last , If the stack is empty , Is legal , If not empty , It's not legal .
#include <iostream> #include <string> #include <stack> #include <cstdio> #
include <map> using namespace std; int main() { int n,i,j; string str; stack<
char> s; map<char,char> mp; // Tie the brackets together mp['(']=')'; mp['{']='}'; mp['[']=']'; mp
[')']='('; mp['}']='{'; mp[']']='['; while(scanf("%d\n",&n)!=EOF){ for(i=0;i<n;i
++){ getline(cin,str); for(j=0;j<str.length();j++){ if(!s.empty()&&mp[s.top()]==
str[j]){ // If the stack is not empty and the character is a good friend of the top element of the stack s.pop(); // The top of the stack element is removed from the stack continue; // And skip the next step }
// If it's a bracket , Then put it on the stack if(str[j]=='('||str[j]=='{'||str[j]=='['||str[j]==')'||str[j]=='}'||
str[j]==']') s.push(str[j]); } if(s.empty()) printf("yes\n"); // Empty stack means legal else
printf("no\n"); // If the stack is not empty, it is illegal while(!s.empty()) s.pop(); // At this time, if the stack is not empty , There will be residues in the stack , The stack should be empty }
} return 0; }

Technology
©2019-2020 Toolsou All rights reserved,
Hikvision - Embedded software written test questions C Language application 0 The length of array in memory and structure is 0 In depth analysis data structure --- The preorder of binary tree , Middle order , Subsequent traversal How to do it ipad Transfer of medium and super large files to computer elementui Shuttle box el-transfer Display list content text too long 2019 The 10th Blue Bridge Cup C/C++ A Summary after the National Games ( Beijing Tourism summary )unity Shooting games , Implementation of first person camera python of numpy Module detailed explanation and application case Study notes 【STM32】 Digital steering gear Horizontal and vertical linkage pan tilt Vue Used in Element Open for the first time el-dialog Solution for not getting element