第2关:利用栈判断字符串括号是否匹配

news/2024/7/7 18:45:05

#ifndef stack__h
#define stack__h#include <stdio.h>
#include <stdlib.h>typedef char T; // 数据元素的数据类型struct Stack{T* data;   // 数据元素存储空间的开始地址int top;   // 栈表的当前位置int max;   // 栈表的最大长度
};Stack* Stack_Create(int maxlen);
// 创建栈void Stack_Free(Stack* stk);
// 释放栈void Stack_MakeEmpty(Stack* stk);
// 置为空栈bool Stack_IsEmpty(Stack* stk);
// 判断栈是否空bool Stack_IsFull(Stack* stk);
// 判断栈是否满T Stack_Top(Stack* stk);
// 获取当前栈顶元素T Stack_Push(Stack* stk, T e);
// 将元素e压入栈顶
// 返回栈顶点元素T Stack_Pop(Stack* stk);
// 将栈顶元素出栈
// 返回栈顶元素void Stack_Print(Stack* stk);
// 打印栈顶到栈低的元素void Bracket_Match(T* str, int len);
//  利用stack栈判断括号是否匹配
//  输入参数:字符串序列,字符串长度
//  若匹配输出YES,否则输出NO,末尾换行#endif /* stack__h */

#include <iostream>
#include <cstring>
#include "stack_.h"int main(int argc, const char * argv[]) {// insert code here...// std::cout << "Hello, World!\n";int len;T* str;scanf("%d", &len);str = (T*)malloc(sizeof(T)*len);scanf("%s", str);Bracket_Match(str, len);return 0;
}

#include "stack_.h"Stack* Stack_Create(int maxlen)
// 创建栈
{Stack* stk = (Stack*)malloc(sizeof(Stack));stk->data = (T*)malloc(sizeof(T)*maxlen);stk->max = maxlen;stk->top = -1;return stk;
}void Stack_Free(Stack* stk)
// 释放栈
{free(stk->data);free(stk);
}void Stack_MakeEmpty(Stack* stk)
// 置为空栈
{stk->top = -1;
}bool Stack_IsEmpty(Stack* stk)
// 判断栈是否空
{return -1 == stk->top;
}bool Stack_IsFull(Stack* stk)
// 判断栈是否满
{return stk->top == stk->max-1;
}T Stack_Top(Stack* stk)
// 获取当前栈顶元素
{return stk->data[stk->top];
}T Stack_Push(Stack* stk, T e)
// 将元素e压入栈顶
// 返回栈顶点元素
{if(Stack_IsFull(stk)) {printf("Stack_IsFull(): stack full error when push element to the stack!\n");Stack_Free(stk);exit(0);}else{stk->top += 1;stk->data[stk->top] = e;return Stack_Top(stk);}
}T Stack_Pop(Stack* stk)
// 将栈顶元素出栈
// 返回栈顶元素
{if(Stack_IsEmpty(stk)) {printf("Stack_IsEmpty(): stack empty error when pop element of the stack top!\n");Stack_Free(stk);exit(0);}else{T topE = Stack_Top(stk);stk->top -= 1;return topE;}
}void Stack_Print(Stack* stk)
// 打印栈顶到栈低的元素
{if (Stack_IsEmpty(stk)) {printf("The stack is empty.\n");return;}//printf("The stack contains: ");for (int i=stk->top; i>=0; i--) {printf("%d", stk->data[i]);}printf("\n");}void Bracket_Match(T* str, int len)
//  利用stack栈判断括号是否匹配
//  输入参数:字符串序列,字符串长度
//  若匹配输出YES,否则输出NO,末尾换行
{Stack*s=Stack_Create(105);for(int i=0;i<len;i++){char st=Stack_Top(s);if(st=='('){if(str[i]==')'){Stack_Pop(s);}else{Stack_Push(s,str[i]);}}else if(st=='['){if(str[i]==']'){Stack_Pop(s);}else{Stack_Push(s,str[i]);}}else if(st=='{'){if(str[i]=='}'){Stack_Pop(s);}else{Stack_Push(s,str[i]);}}else{Stack_Push(s,str[i]);}}if(Stack_IsEmpty(s)){printf("YES\n");}else{printf("NO\n");}}

http://lihuaxi.xjx100.cn/news/285913.html

相关文章

Codeforces Round #645 (Div. 2)(D.The Best Vacation)

题目链接&#xff1a;https://codeforces.com/contest/1358/problem/D 思路&#xff1a;双指针前缀和 前缀和主要处理了两组数据&#xff1a;sum[]是某月到某月的天数,ans[] 代表某月到某月能得到得hug数 然后我们通过双指针的方法来遍历整个范围&#xff0c;当sum[r]-sum[l-1]…

如何用最强模型BERT做NLP迁移学习?

作者 | 台湾大学网红教授李宏毅的三名爱徒来源 | 井森堡&#xff0c;不定期更新机器学习技术文并附上质量佳且可读性高的代码。编辑 | Jane谷歌此前发布的NLP模型BERT&#xff0c;在知乎、Reddit上都引起了轰动。其模型效果极好&#xff0c;BERT论文的作者在论文里做的几个实验…

第3关:利用栈判断字符串是否为回文串

#ifndef stack__h #define stack__h#include <stdio.h> #include <stdlib.h>typedef char T; // 数据元素的数据类型struct Stack{T* data; // 数据元素存储空间的开始地址int top; // 栈表的当前位置int max; // 栈表的最大长度 };Stack* Stack_Create(int …

并查集 ---- 扩展域并查集判二分图 + 循环模拟字典树 The 2020 ICPC Asia Macau Regional Contest C. Club Assignment (详解)

题目链接 题目大意&#xff1a; 有n个数&#xff0c;现在要把他们拆分成两个集合&#xff0c;假设S为集合&#xff0c;有如下定义&#xff1a; f(S){min(x⊕y)∣x,y∈S,andx!y}f(S)\{min(x\oplus y)|x,y\in S,and\;x!y\}f(S){min(x⊕y)∣x,y∈S,andx!y} 将n个数拆分为两个集合…

实现Date函数属性中的format方法

js中没有Date.format方法的&#xff0c;所以在date属性中加format方法 //js格式化属性 Date.prototype.format function (format) {   var o {     "M": this.getMonth() 1, //month     "d": this.getDate(), //day     "h": …

网易开源支持图像识别的自动化UI测试工具,零基础亲测好评!

编辑 | Jane出品 | AI科技大本营AI科技大本营给大家推荐了很多有意思、适合开发者们的工具&#xff0c;比如代码修复神器、帮小白快速分析 Error、PDF 翻译工具、变量命名神器等等。今天&#xff0c;营长要专门给测试人员&#xff0c;或者想做测试的小伙伴们推荐一款工具&#…

[CQOI2009]中位数图 详细题解

题目链接&#xff1a; https://ac.nowcoder.com/acm/problem/19913 题目描述&#xff1a; 给出1~n的一个排列&#xff0c;统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后&#xff0c;位于中间的数。 题解&#xff1a; 因为中位数是…

容斥 + 爆搜打表 ---- 2020年南京icpc H.Harmonious Rectangle

题目链接 题目大意&#xff1a; 就是给你一个二维平面{(x,y)∣1≤x≤n,1≤y≤m}\{(x,y)|1\leq x\leq n,1\leq y \leq m\}{(x,y)∣1≤x≤n,1≤y≤m},你现在有3种颜色&#xff0c;你可以给写平面的点涂上颜色&#xff0c;问你至少存在4个点(x1,y1),(x2,y2),(x1,y2),(x2,y1)(x1,y…