sdut 数据结构实验之栈四:括号匹配
生活随笔
收集整理的這篇文章主要介紹了
sdut 数据结构实验之栈四:括号匹配
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)之棧四:括號(hào)匹配
Time Limit:?1000MS?Memory Limit:?65536KB Submit?Statistic?DiscussProblem Description
給你一串字符,不超過(guò)50個(gè)字符,可能包括括號(hào)、數(shù)字、字母、標(biāo)點(diǎn)符號(hào)、空格,你的任務(wù)是檢查這一串字符中的( ) ,[ ],{ }是否匹配。Input
輸入數(shù)據(jù)有多組,處理到文件結(jié)束。Output
如果匹配就輸出“yes”,不匹配輸出“no”Example Input
sin(20+10){[}]Example Output
yesno
每種情況都要考慮到
#include<iostream> #include<stack> #include<cstring> #include<cstdio> using namespace std;int main() {char ch[1000],str[1000];while(gets(str)){int p=0;for(int i=0;i<strlen(str);i++){if(str[i]=='{'||str[i]=='}'||str[i]=='('||str[i]==')'||str[i]=='['||str[i]==']'){ch[p++]=str[i];}}//puts(ch);int a=0,b=0,c=0,l=1;for(int i=0;i<p;++i){if(!l) break;switch(ch[i]){case '(':a++;break;case ')':if(a)a--;else l=0;break;case '{':b++;break;case '}':if(b)b--;elsel=0;break;case '[':c++;break;case ']':if(c)c--;elsel=0;break;}}int k=0;//printf("-%d-\n",l);if((a==0&&b==0&&c==0)&&l){for(int i=0;i<strlen(ch)-1;i++){if(ch[i]=='{'&&ch[i+1]==']'||ch[i]=='{'&&ch[i+1]==')'||ch[i]=='['&&ch[i+1]==')'||ch[i]=='['&&ch[i+1]=='}'||ch[i]=='('&&ch[i+1]==']'||ch[i]=='('&&ch[i+1]=='}'){k=1;printf("no\n");}}if(!k)printf("yes\n");}elseprintf("no\n");}return 0; }總結(jié)
以上是生活随笔為你收集整理的sdut 数据结构实验之栈四:括号匹配的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c# MEF框架(四 见证奇迹的时刻之实
- 下一篇: node.js事件