Bracket Matching(Data structure)
来源:互联网 发布:网络直播平台土豪排名 编辑:程序博客网 时间:2024/06/05 16:01
Check that the bracket in the input string is matched.The brackets include {,},[,],(,).
Input
The first line is an integer , namely N,which is the number of test cases . The following N lines, each line is a string no longer than 100 characters.
Output
First line of Output is an integer, namely N. number of test cases The following N lines, each line is a string of length not exceeding 100.
It is a simple and easy program,that we can just traverse the numbers and push the left brackets into a stack
,when we run into the right brackets, have a comparison with the top element in the stack.Meanwhie,if the bracket is matched to the top element,then pop the top element, otherwise, continue to traverse. After the traverse, if the stack
is empty, the case is matched.
code for the question(Hope make you some help)
/*@csdn_cinderella wrote at 2016.9.21 22:29*/nclude<iostream>#include<string>#include<stack>using namespace std;int main() { int n; cin >> n; while (n--) { string str; stack<char> temp; cin >> str; int size = str.length(); for (int i = 0; i < size; i++) { if (str[i] == '(' || str[i] == '{' || str[i] == '[') { temp.push(str[i]); } else if (str[i] == ')') { if (temp.empty()) { temp.push(str[i]); } else { if (temp.top() == '(') { temp.pop(); } else { temp.push(str[i]); } } } else if (str[i] == '}') { if (temp.empty()) { temp.push(str[i]); } else { if (temp.top() == '{') { temp.pop(); } else { temp.push(str[i]); } } } else if (str[i] == ']') { if (temp.empty()) { temp.push(str[i]); } else { if (temp.top() == '[') { temp.pop(); } else { temp.push(str[i]); } } } } if (temp.empty()) { cout << "Yes" << endl; } else { cout << "No" << endl; } }}
- Bracket Matching(Data structure)
- 1010. bracket matching
- 1004. Bracket Matching
- 1004. Bracket Matching
- Data Structure
- Data structure
- Data Structure
- Data structure
- Data structure
- data structure
- Data Structure
- Data structure
- Data Structure
- Data Structure
- Data Structure
- Data Structure
- Data structure alignmentData structure alignment
- Finish Data Structure Examination
- GATHER:基因注释工具
- HDU5894【组合数学】
- Tmux 快捷键
- 过滤器与拦截器
- 使用sqlyog导入数据表到MySql中
- Bracket Matching(Data structure)
- Spark2.0 Structured Streaming
- verilog中读写文件操作
- 【xxnet】xxnet使用总结
- 拓展欧几里得模板
- 小站使用的ECS机器部署,无法通过360网站安全检测,求解,多多儿小说网
- 移动端像素的深究
- android机型适配终极篇
- hdoj5493【树状数组+二分】