括号配对问题

来源:互联网 发布:国家一级美术师数据库 编辑:程序博客网 时间:2024/05/16 23:55
<div class="problem-display" style="font-size: 14px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><h2 style="margin: 0px; padding: 0px; font-size: 18px; text-align: center; color: rgb(113, 32, 21); font-family: 微软雅黑, 黑体;">括号配对问题</h2><div class="problem-ins" style="text-align: center;">时间限制:<span class="editable highlight" id="problem[time_limit]" style="color: rgb(113, 32, 21);">3000</span> ms  |  内存限制:<span class="editable highlight" id="problem[memory_limit]" style="color: rgb(113, 32, 21);">65535</span> KB</div><div class="problem-ins" style="text-align: center;">难度:<span class="editable highlight" style="color: rgb(113, 32, 21);">3</span></div></div><div class="clr" style="clear: both; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun; font-size: 13px; line-height: 19.5px;"></div><dl class="problem-display" style="margin: 0px; padding: 0px; font-size: 14px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">描述</dt><dd style="margin: 0px; padding: 0px;">现在,有一行括号序列,请你检查这行括号是否配对。</dd><div class="clr" style="clear: both;"></div><dl class="others" style="margin: 0px; padding: 0px;"><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">输入</dt><dd style="margin: 0px; padding: 0px;">第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符</dd><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">输出</dt><dd style="margin: 0px; padding: 0px;">每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No</dd><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">样例输入</dt><dd style="margin: 0px; padding: 0px;"><pre id="sample_input" style="margin-top: 0px; margin-bottom: 0px; padding: 5px 10px; font-family: Consolas, 'Courier New', 'DejaVu Sans Mono', 'Droid Sans Mono', monospace; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); min-height: 20px; line-height: 1.5em;">3[(])(])([[]()])
样例输出
NoNoYes

#include<iostream>using namespace std;class Stack{private:char data[10000];char a;public:int top,base;Stack(){base = top = 0;}~Stack();void Stack1(char &);void show();};void Stack::Stack1(char &c){switch (c){case '[':if (top<10000){data[top] = c;++top;}break;case '(':if (top<10000){data[top] = c;++top;}break;case ')':a = data[top-1];if (a == '(' && top!=base)--top;else{data[top] = c;++top;}break;case']':a = data[top-1];if (a == '[' && top!=base)--top;else{data[top] = c;++top;}break;}}void Stack::show()//输出结果{if(base==top)cout<<"Yes"<<endl;elsecout<<"No"<<endl;}int main(){ int N,i;Stack *S=new Stack();cin>>N;char x[10000]={""};while(N--){cin>>x;for(i=0;x[i]!='\0';++i){S->Stack1(x[i]);}S->show();S->base=S->top=0;}return 0;}

0 0