C++ STL stack 括号匹配 源代码

// STL_stack.cpp : 定义控制台应用程序的入口点。
// STL 栈stack

#include "stdafx.h"
#include <iostream>
#include <stack>
#include <map>
#include <string>
#include <time.h>
using namespace std;

multimap<char, char> match;
multimap<char, char> trueMatch;
typedef pair<multimap<char, char>::iterator, multimap<char, char>::iterator> pairItr;
stack<char> st;
void initMatch(){
/*
\ ( [ { } ] )
( 2 2 2 0 0 1
[ 2 2 2 0 1 0
{ 2 2 2 1 0 0
} 0 0 1 0 0 0
] 0 1 0 0 0 0
) 1 0 0 0 0 0
*/

match.insert(make_pair(‘(‘, ‘)‘));
match.insert(make_pair(‘(‘, ‘[‘));
match.insert(make_pair(‘(‘, ‘{‘));
match.insert(make_pair(‘(‘, ‘(‘));

match.insert(make_pair(‘[‘, ‘]‘));
match.insert(make_pair(‘[‘, ‘(‘));
match.insert(make_pair(‘[‘, ‘[‘));
match.insert(make_pair(‘[‘, ‘{‘));

match.insert(make_pair(‘{‘, ‘}‘));
match.insert(make_pair(‘{‘, ‘{‘));
match.insert(make_pair(‘{‘, ‘[‘));
match.insert(make_pair(‘{‘, ‘(‘));

match.insert(make_pair(‘)‘, ‘(‘));
match.insert(make_pair(‘]‘, ‘[‘));
match.insert(make_pair(‘}‘, ‘{‘));
/*---------------------------*/
trueMatch.insert(make_pair(‘(‘, ‘)‘));
trueMatch.insert(make_pair(‘)‘, ‘(‘));
trueMatch.insert(make_pair(‘]‘, ‘[‘));
trueMatch.insert(make_pair(‘[‘, ‘]‘));
trueMatch.insert(make_pair(‘{‘, ‘}‘));
trueMatch.insert(make_pair(‘}‘, ‘{‘));
}

bool isMatch(char ch){

if (st.empty()){
st.push(ch);
return true;
}

pairItr pair = match.equal_range(st.top());
bool isExit = true;
for (auto itr = pair.first; itr != pair.second;itr++)
{
if ((*itr).second == ch){
isExit = false;
break;
}
}
if (isExit){
return false;
}

if(trueMatch.find(st.top()) != trueMatch.end() && (*trueMatch.find(st.top())).second == ch){
st.pop();
return true;
}
st.push(ch);
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
clock_t begin_clock_t = clock();
initMatch();
//最糟糕情况 匹配情况 最后一位不匹配
//string str = "([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])[";
string str = "([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])([{}])";

for (auto var = str.begin(); var != str.end();var++)
{
if(!isMatch(*var))break;
}
if (st.empty()){
cout << "----------匹配-----------" << endl;
}
else{
cout << "不匹配" << endl;
}
clock_t end_clock_t = clock();
cout << double(end_clock_t - begin_clock_t) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
getchar();
return 0;
}

原文地址:http://blog.51cto.com/whish/2066907

时间: 2024-08-29 22:58:31

C++ STL stack 括号匹配 源代码的相关文章

STL版 括号匹配(感觉不如之前自己用数组模拟的跑的快)

数据结构实验之栈四:括号匹配 Time Limit: 1000MS Memory limit: 65536K 题目描述 给你一串字符,不超过50个字符,可能包括括号.数字.字母.标点符号.空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配. 输入 输入数据有多组,处理到文件结束. 输出 如果匹配就输出“yes”,不匹配输出“no” 示例输入 sin(20+10) {[}] 示例输出 yes no 注意:读入的字符串里可能含有空格哦! #include <iostream> #i

详解括号匹配问题(STL stack)

1. 括号匹配的四种可能性: ①左右括号配对次序不正确 ②右括号多于左括号 ③左括号多于右括号 ④左右括号匹配正确 2. 算法思想: 1.顺序扫描算数表达式(表现为一个字符串),当遇到三种类型的左括号时候让该括号进栈: 2.当扫描到某一种类型的右括号时,比较当前栈顶元素是否与之匹配,若匹配,退栈继续判断: 3.若当前栈顶元素与当前扫描的括号不匹配,则左右括号配对次序不正确,匹配失败,直接退出: 4.若字符串当前为某种类型的右括号而堆栈已经空,则右括号多于左括号,匹配失败,直接退出: 5.字符串循

The application of the stack—Parentheses Matching(括号匹配)

The thought of the algorithm is as follows: (1) Initially set up an empty stack, sequentially read in parentheses; (2) If it is a right parentheses, or it matches the stack top element, or it is illegal; (3) If it is a left parentheses, it will be pu

rwkj 1378 stack(括号匹配)

C++:泛型编程stack(括号匹配)时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:72 测试通过:39 描述 假设表达式中包含一种括号:圆括号,其嵌套顺序随意,即(()())或(())等为正确的格式,)(或((())或())均为不正确的格式.检验括号是否匹配可以用堆栈来实现当遇到 ( 时进栈,遇到 ) 时出栈进行匹配检验,如果出现不匹配的情况立即结束,否则继续取下一个字符.如果没有遇到不匹配的情况,最后判断栈是否为空,栈为空,括号匹配,否则不

rwkj 1378 C++:泛型编程stack(括号匹配)

C++:泛型编程stack(括号匹配) 时间限制(普通/Java):1000MS/3000MS            运行内存限制:65536KByte 总提交:176            测试通过:88 描述 假设表达式中包含一种括号:圆括号,其嵌套顺序随意,即(()())或(())等为正确的格式,)(或((())或())均为不正确的格式.检验括号是否匹配可以用堆栈来实现当遇到 ( 时进栈,遇到 ) 时出栈进行匹配检验,如果出现不匹配的情况立即结束,否则继续取下一个字符.如果没有遇到不匹配的

堆栈_括号匹配

class Solution { public: bool isValid(string s) { if(s.empty()) return false; stack<int> s1; int n=s.size(); for(int i=0;i<n;i++) { if(s[i]=='('||s[i]=='['||s[i]=='{') s1.push(s[i]); else if(s1.empty()) return false; else if((s[i]==')'&&s

行编辑程序、括号匹配检验

行编辑程序.括号匹配检验程序都是利用的栈的数据结构.而这两个 小程序也非常好的显示了栈先进后出的思想.由于程序本身很简短.清晰,所 以也就不做多的解释了,直接上代码了. 行编辑程序: #include<iostream> #include<stack> using namespace std; int main() { stack<char> sta; char ch = getchar(); while(ch!=EOF) { while(ch!=EOF&&am

题目1153:括号匹配问题(栈的使用)

题目链接:http://ac.jobdu.com/problem.php?pid=1153 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus // // 1153 括号匹配问题.cpp // Jobdu // // Created by PengFei_Zheng on 08/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h>

栈的应用-判断括号匹配

栈的一个典型应用就是可以用来协助分析表达式的括号是否匹配.括号可以延伸到任何成对出现的界定符,例如引号,书名号等. 接下来给出程序实现: 第一部分给出的是堆栈的插入,删除等操作对应的实现: 1 public class StackChar 2 { 3 private int maxSize;//堆栈数组大小 4 private char [] stackArray; 5 private int top;//堆栈顶 6 public StackChar(int maxSize) 7 { 8 thi