栈——括号匹配

Scaena Felix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 492    Accepted Submission(s): 214

Problem Description

Given a parentheses sequence consist of ‘(‘ and ‘)‘, a modify can filp a parentheses, changing ‘(‘ to ‘)‘ or ‘)‘ to ‘(‘.

If we want every not empty <b>substring</b> of this parentheses sequence not to be "paren-matching", how many times at least to modify this parentheses sequence?

For example, "()","(())","()()" are "paren-matching" strings, but "((", ")(", "((()" are not.

Input

The first line of the input is a integer T, meaning that there are T test cases.

Every test cases contains a parentheses sequence S only consists of ‘(‘ and ‘)‘.

1≤|S|≤1,000.

Output

For every test case output the least number of modification.

Sample Input

3
()
((((
(())

Sample Output

1
0
2

题目最终就是要要求统计的匹配括号对,实际就是栈的应用。

源码:

#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<string>

#include<stack>

#include<vector>

using namespace std;

#define MAX 1009

template <class Type>

class Stack {

public:

Stack(int MaxStackSize = MAX);

bool IsFull();

bool IsEmpty();

void Push(const Type x);

Type Pop();

private:

int top;

Type *stack;

int MaxSize;

};

template<class Type>

Stack<Type>::Stack(int MaxStackSize) :MaxSize(MaxStackSize)

{

stack = new Type[MaxSize];

top = -1;

}

template<class Type>

inline bool Stack<Type>::IsFull()

{

if (top == MaxSize - 1)

return true;

else

return false;

}

template<class Type>

inline bool Stack<Type>::IsEmpty()

{

if (top == -1)

return true;

else

return false;

}

template<class Type>

void Stack<Type>::Push(const Type x)

{

if (IsFull())

;

else

stack[++top] = x;

}

template<class Type>

Type Stack<Type>::Pop()

{

if (IsEmpty())

{

return 0;

}

else

{

Type x = stack[top--];

return x;

}

return 0;

}

template<class Type>

int GetNum(const Type *S,int n)

{

int count = 0;

Type x = ‘ ‘;

Stack<Type> ST;

ST.Push(*S++);

for (int i = 1; i < n; i++)

{

x = ‘ ‘;

if (!ST.IsEmpty())

{

x = ST.Pop();

}

if (x == ‘(‘&&*S == ‘)‘)

{

count++;

*S++;

}

else

{

if (x!=‘ ‘)

ST.Push(x);

ST.Push(*S++);

}

}

return count;

}

int main()

{

int T = 0;

string S;

while (cin >> T) {

for (int i = T; i >= 1; i--)

{

cin >> S;

cout << GetNum(S.c_str(),S.size()) << endl;

}

}

return 0;

}

时间: 2025-01-20 03:40:14

栈——括号匹配的相关文章

数据结构上机3栈-括号匹配

#include <stdio.h> #include <malloc.h> #define OK 1 #define OVERFLOW -1 #define ERROR 0 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef char SElemType; typedef int Status; typedef struct{ SElemType * base; SElemType * top; int st

栈&amp;&amp;括号匹配

利用栈的结构性质实现括号匹配 (今天被自己玩死了,'='&&'=='小问题坑死人不偿命) Matbr.h #ifndef _MATCH_H_ #define _MATCH_H_ #include<iostream> #include<string.h> #include<assert.h> using namespace std; typedef char ElemType; #define STACK_MAX_SIZE 100 typedef stru

C语言实现 数据结构-栈-括号匹配

题目描述 假设一个表达式有英文字母(小写).运算符(+,—,*,/)和左右小(圆)括号构成,以“@”作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返回“YES”:否则返回“NO”.假设表达式长度小于255,左圆括号少于20个. 输入 一行字符串 输出 YES或者NO 样例输入 ((1+b)-(a+3))*[email protected] 样例输出 YES 样例输入 (25+x)*(a*(a+b+b)@ 样例输出 NO 算法实现:1.利用栈来实现只需要考虑输入的是

括号匹配问题(顺序栈实现)

本周老师作业留了两个.先上传一个吧.那个有时间我再传上来~ 本周的要求: 1.给出顺序栈的存储结构定义. 2.完成顺序栈的基本操作函数. 1)      初始化顺序栈 2)      实现入栈和出栈操作 3)      实现取栈顶元素和判空操作 括号匹配问题 3.编写主函数实现基本操作函数功能,并设置测试数据,测试合法和非法数据的输出结果. 4.程序调试运行并保存输出结果. 5.整理并提交实验作业. 1 #include <cstdio> 2 #include <cstring>

栈的应用-判断括号匹配

栈的一个典型应用就是可以用来协助分析表达式的括号是否匹配.括号可以延伸到任何成对出现的界定符,例如引号,书名号等. 接下来给出程序实现: 第一部分给出的是堆栈的插入,删除等操作对应的实现: 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

栈的两个应用:括号匹配的检验和表达式求值

1.     括号匹配的检验 假设表达式中含有3种括号:(),[],{},其嵌套的顺序随意.检验括号是否匹配. 基本思想:在算法中设置一个栈,每读入一个括号,若是右括号,则或者与栈顶匹配的左括号相互消解,或者是不合法的情况:若是左括号,则直接压入栈中.若括号匹配,在算法的开始和结束时,栈都应该是空的. 代码: /* * 判断表达式中的括号是否匹配,匹配输出Yes,否则输出No * {(zhang)+[lei]+{lei}={zhangleilei}} -> Yes * {(zhang)+[lei

括号匹配为题(栈的思想)哈

数据结构实验之栈四:括号匹配 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 给你一串字符,不超过50个字符,可能包括括号.数字.字母.标点符号.空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配. 输入 输入数据有多组,处理到文件结束. 输出 如果匹配就输出“yes”,不匹配输出“no” 示例输入 sin(20+10) {[}] 示例输出 yes no 提示 我的分析:运用栈的思想,读到左边的括号,就把它推进

数据结构复习_栈和队列,应用_括号匹配&amp;文件目录递归拷贝,

1.主题 2.学习视频和资料 视频    http://study.163.com/course/courseLearn.htm?courseId=555010#/learn/video?lessonId=701019&courseId=555010 http://study.163.com/course/courseLearn.htm?courseId=555010#/learn/video?lessonId=702024&courseId=555010 3.实现 数组或列表实现 4.应

C数据结构-栈和队列,括号匹配举例

1.栈和队列是两种特殊的线性表 运算操作被限定只能在表的一端或两端插入,删除元素,故也称它们为限定的线性表结构 2.栈的基本运算 1).Stackinit(&s) 构造一个空栈 2).Stackempty(s) 判断s是否为空栈,当s为空栈时,函数返回值1 否则 0 3).Push(&s,x)  在栈s 的顶部插入元素x,简称将x入 栈 4).Pop(&s,&x) 在栈s 中删除顶元并将其值保存在x单元中返回,简称将x出栈 5)Gettop(s,&x)  读s栈中的