括号匹配问题(C++ 栈)

#include<iostream>
#include<string>
#include<stack>
#include<map>
using namespace std;

bool judge(string s) {
	map<char, char> map_s = { {'(',')'},{'[',']'},{'{','}'} };
	stack<char> temp;

	for (int i = 0; i < s.length(); ++i) {
		if (map_s.find(s[i]) != map_s.end()) 
			temp.push(map_s[s[i]]);
		else {
			if (temp.empty())
				return false;
			if (s[i] == temp.top())
				temp.pop();
			else
				return false;
		}
	}
	if (temp.empty())
		return true;
	else
		return false;
}
int main()
{
	string str;
	while (1)
	{
		getline(cin, str);
		if (str.length() == 0) break;
		if (judge(str)) puts("yes");
		else puts("no");
	}
	system("pause");
	return 0;
}

 

    原文作者:括号匹配问题
    原文地址: https://blog.csdn.net/weixin_43411988/article/details/85038076
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞