The following program is my ACcepted code for UVA-673 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UVA 673 的AC code!
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw)
If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UVA 673 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This program is for UVa 673 Parentheses Balance | |
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=8&page=show_problem&problem=614 | |
#include<stdio.h> | |
#include<stack> | |
#include<string.h> | |
#include<iostream> | |
using namespace std; | |
int main(){ | |
int n; | |
char str[130];//max string is 127 | |
stack<char> paren; | |
bool ans; | |
scanf("%d", &n); | |
getchar(); | |
while(n--){ | |
while(!paren.empty()) | |
paren.pop(); | |
ans = true; | |
gets(str);//because the empty string is legal! | |
int len = strlen(str); | |
for(int i = 0; i < len; i++){ | |
if(str[i] == '(' || str[i] == '['){ | |
paren.push(str[i]); | |
} | |
else{//str[i] = ')' or ']' | |
if(paren.empty()){//the stack is empty, but there's a end parentheses | |
ans = false; | |
break; | |
} | |
char tmp = paren.top(); | |
paren.pop(); | |
//printf("%c %c\n", tmp, str[i]);//for checking | |
if(tmp == '(' && str[i] != ')'){ | |
ans = false; | |
break; | |
} | |
else if(tmp == '[' && str[i] != ']'){ | |
ans = false; | |
break; | |
} | |
} | |
} | |
if(!paren.empty())//the stack is still having something | |
ans = false; | |
if(ans) | |
printf("Yes\n"); | |
else | |
printf("No\n"); | |
} | |
return 0; | |
} |
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!