用栈实现成对括号检查 (Check bracket base on Stack)

本文介绍了一个简单的C++程序,用于检查字符串中括号(包括圆括号、方括号和花括号)是否正确配对。通过使用顺序栈的数据结构实现括号匹配的算法,并给出了完整的代码示例。

问题描述:从左至右读取表达式,读到运算对象和运算符,不做任何动作,直接往下继续读,读到括号,要对括号的使用是否正确进行检查。
如:   {[ ]( [ ][ ])}   为合法

代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
#define MAXSIZE 20
typedef struct SeqStack
{
	char data[MAXSIZE];
	int top;
}SeqStack;

int Bracket_Check(char *a)
{
	int i;
	SeqStack s;
	s.top = -1;
	for (i = 0; a[i] != '\0'; i++)
	{
		if (a[i] == '(' || a[i] == '[' || a[i] == '{')
			s.data[++s.top] = a[i];
		if (a[i] == ')' || a[i] == ']' || a[i] == '}')
		{
			if ((a[i] == ')' && s.data[s.top] == '(') || (a[i] == ']' && s.data[s.top] == '[') || (a[i] == '}' && s.data[s.top] == '{'))
				s.top--;
			else
				return 0;		
		}
	}
	if (s.top == -1)
		return 1;
	else
		return 0;
}

int main()
{
	char input[MAXSIZE];
	int check;
	gets_s(input, MAXSIZE);
	check = Bracket_Check(input);
	if (check)
		printf("Yes\n");
	else
		printf("No\n");
	system("pause");
}
编译环境: Visual Studio
C - Brackets Stack Query / Time Limit: 3 sec / Memory Limit: 1024 MiB Score : 300 points Problem Statement A string T is called a good bracket sequence if and only if it satisfies the following condition: T can be made into an empty string by repeating the following operation zero or more times: Choose () contained in T as a (contiguous) substring and remove it. For example, (), (()()), and the empty string are good bracket sequences, but )()( and ))) are not good bracket sequences. There is a string S. Initially, S is an empty string. Process Q queries in the order they are given. After each query, determine whether S is a good bracket sequence. There are two types of queries: 1 c: A character c is given. c is either ( or ). Append c to the end of S. 2: Remove the last character of S. It is guaranteed that S is not an empty string at this time. Constraints 1≤Q≤8×10 5 c in queries of the first type is ( or ). It is guaranteed that S is not empty when a query of the second type is given. Q is an integer. Input The input is given from Standard Input in the following format, where query i ​ denotes the i-th query. Q query 1 ​ query 2 ​ ⋮ query Q ​ Each query is given in one of the following two formats: 1 c 2 Output Output Q lines. The i-th line should contain Yes if the string S immediately after processing the i-th query is a good bracket sequence, and No otherwise. Sample Input 1 Copy 8 1 ( 2 1 ( 1 ) 2 1 ( 1 ) 1 ) Sample Output 1 Copy No Yes No Yes No No No Yes S immediately after processing the 1st query is (, which is not a good bracket sequence. S immediately after processing the 2nd query is an empty string, which is a good bracket sequence. S immediately after processing the 3rd query is (, which is not a good bracket sequence. S immediately after processing the 4th query is (), which is a good bracket sequence. S immediately after processing the 5th query is (, which is not a good bracket sequence. S immediately after processing the 6th query is ((, which is not a good bracket sequence. S immediately after processing the 7th query is ((), which is not a good bracket sequence. S immediately after processing the 8th query is (()), which is a good bracket sequence.c++
最新发布
10-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值