括号匹配
2015-10-25 11:19
218 查看
#include <stdio.h> #include <string.h> char a[31],name[31]; int b[31],num,top; void getname() { int i,popnum; char tmp; popnum = 0; for(i = 0; name[i] != 0; i++) { if(name[i] == '[' ||name[i] == '(') { a[++top] = name[i]; } else { if(name[i] == ']' && a[top] == '[' ) { if(b[top+1] == 0) b[top] = 3 + b[top]; else { b[top] = 3* b[top+1] +b[top]; b[top+1] = 0; } top--; } else if(name[i] ==')' && a[top] == '(') { if(b[top+1] == 0) b[top] = 2 + b[top]; else { b[top] = 2* b[top+1]+b[top]; b[top+1] = 0; } top--; } else { num = 0; break; } } } if(a[top] != 0) b[top+1] = 0; } int main(void) { int tc, T; freopen("input.txt", "r", stdin); setbuf(stdout, NULL); scanf("%d", &T); for(tc = 0; tc < T; tc++) { memset(name, 0, sizeof(name)); memset(b, 0, sizeof(b)); scanf("%s", name); top = num = 0; getname(); printf("%d\n", b[top+1]); } return 0;//Your program should return 0 on normal termination. }
相关文章推荐
- python正则表达式中的括号匹配问题
- 括号匹配问题
- 打印括号匹配的所有排列
- if语句中的括号匹配检测
- Parentheses Balance——括号匹配
- 愚人节的礼物
- 伪代码:括号匹配
- [栈和队列]括号匹配
- 括号匹配算法的C++代码实现
- C++STL库栈算法的括号匹配
- 检验某符号(如圆括号,方括号,花括号等)是否都成对出现
- (C++)数据结构之括号匹配的问题?
- 括号匹配
- LeetCode 20 - Valid Parentheses
- 括号匹配问题,用栈实现
- LeetCode——Valid Parentheses
- LeetCode——Generate Parentheses
- 看数据结构写代码(11)栈的应用(二) 括号匹配的检查
- NYOJ 15 括号匹配(二) dp
- toj1242 Parencodings