数据结构实验之栈与队列四:括号匹配
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。
Input
输入数据有多组,处理到文件结束。
Output
如果匹配就输出“yes”,不匹配输出“no”
Example Input
sin(20+10) {[}]
Example Output
yes no
Hint
Author
ma6174
#include <stdio.h> #include <stdlib.h> int main() { int top,i; char a[51],s[51],k; while(gets(s)) { top=-1; for(i=0; s[i]!='\0'; i++) { if(s[i]=='{'||s[i]=='['||s[i]=='(') { top++; a[top]=s[i]; } if(s[i]=='}'||s[i]==']'||s[i]==')') { k=a[top]; top--; if((k=='{'&&s[i]=='}')||(k=='('&&s[i]==')')||(k=='['&&s[i]==']')) ; else break; } } if(top==-1&&s[i]=='\0') printf("yes\n"); else printf("no\n"); } return 0; }