日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sdut 2134 数据结构实验之栈与队列四:括号匹配

發布時間:2025/4/16 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sdut 2134 数据结构实验之栈与队列四:括号匹配 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據結構實驗之棧與隊列四:括號匹配

Time Limit:?1000MS?Memory Limit:?65536KB Submit?Statistic?Discuss

Problem Description

?給你一串字符,不超過50個字符,可能包括括號、數字、字母、標點符號、空格,你的任務是檢查這一串字符中的( ) ,[ ],{ }是否匹配。

?

Input

?輸入數據有多組,處理到文件結束。

?

Output

?如果匹配就輸出“yes”,不匹配輸出“no”

?

Example Input

sin(20+10) {[}]

Example Output

yes no 利用棧的思想遇到({ [時進棧,遇到)] }就看棧頂元素與之是否匹配,匹配棧頂元素則出棧,否則 false? #include <iostream> #include <cstring> #include <stdio.h> using namespace std; int tag; int match(char ST[],char ch,int &k) {if(ch==')'&&ST[k]=='('||ch==']'&&ST[k]=='['||ch=='}'&&ST[k]=='{'){tag=1;k--;}elsetag=0;} int main() {char ch[52];while(gets(ch)){char ST[52];int k=-1;for(int i=0;ch[i]!='\0';++i){switch(ch[i]){case '(':case '[':case '{':ST[++k]=ch[i];break;case ')':case ']':case '}': match(ST,ch[i],k);break;}}if(tag&&k==-1)cout<<"yes"<<endl;elsecout<<"no"<<endl;}return 0; }下面的是剛開始寫的忘記考慮 ( ) ( )這種情況了 #include <iostream> #include <cstring> #include <stdio.h> using namespace std; int match(char ST[],int k) {if(k==0||k%2!=0){cout<<"no"<<endl;return 0;}int j=k-1;int i;for(i=0;i<(k/2);i++){switch(ST[i]){case '(':if(ST[j]==')')j--;else{cout<<"no"<<endl;return 0;}break;case '[':if(ST[j]==']')j--;else{cout<<"no"<<endl;return 0;}break;case '{':if(ST[j]=='}')j--;else{cout<<"no"<<endl;return 0;}break;default :{cout<<"no"<<endl;return 0;}break;}}//if(i==k/2)cout<<"yes"<<endl;return 0; } int main() {char ch[52];while(gets(ch)){char ST[52];int k=0;for(int i=0;ch[i]!='\0';++i){switch(ch[i]){case '(':case '[':case '{':case ')':case ']':case '}':ST[k++]=ch[i];break;}}ST[k]='\0';match(ST,k);}return 0; }/*************************************************** User name: YT1658506207邵雪源 Result: Wrong Answer Take time: 0ms Take Memory: 204KB Submit time: 2017-10-10 17:56:27 ****************************************************/


總結

以上是生活随笔為你收集整理的sdut 2134 数据结构实验之栈与队列四:括号匹配的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。