正文
增加 9 行删除 9 行
6 行未改动7891011121314151617181920212223242526272829303132333435365 行未改动6 行未改动### 参考程序```cpp#include <iostream>#include <stack> //包含容器stack#include <string>using namespace std;stack < char > p; //定义一个栈容器 p,存储char类型元素string s;int main(){p.push('#'); //前面加一个“哨兵”,避免访问空栈cin>>s;for(int i=0;i<s.size();i++){if(s[i]=='(' || s[i]=='[')p.push(s[i]); //当前字符为左括号则进栈else{ //当前字符为右括号if(s[i]==')' && p.top()=='(' || s[i]==']' && p.top()=='[')p.pop(); //与栈顶的左括号匹配成功,弹出栈顶else{cout<<"no"<<endl; //匹配失败,输出“no”并结束程序return 0;}}}if(p.top()!='#')cout<<"no"<<endl; //栈中还有多余的左括号,匹配失败,输出“no”else cout<<"yes"<<endl; //全部匹配成功,输出“yes”return 0;}```5 行未改动6 行未改动7891011121314151617181920212223242526272829303132333435365 行未改动6 行未改动### 参考程序```cpp#include <iostream>#include <stack> //包含容器stack#include <string>using namespace std;stack < char > p; //定义一个栈容器 p,存储char类型元素string s;int main(){p.push('#'); //前面加一个“哨兵”,避免访问空栈cin>>s;for(int i=0;i<s.size();i++){if(s[i]=='(' || s[i]=='[')p.push(s[i]); //当前字符为左括号则进栈else{ //当前字符为右括号if(s[i]==')' && p.top()=='(' || s[i]==']' && p.top()=='[')p.pop(); //与栈顶的左括号匹配成功,弹出栈顶else{cout<<"no"<<endl; //匹配失败,输出“no”并结束程序return 0;}}}if(p.top()!='#')cout<<"no"<<endl; //栈中还有多余的左括号,匹配失败,输出“no”else cout<<"yes"<<endl; //全部匹配成功,输出“yes”return 0;}```5 行未改动