STL之stack

栈 (stack)又称堆栈,是一种受限制的线性表,其限制是只允许在表的一端进行插入和删除。
允许操作的一端称为栈顶(top),不允许 操作的称为栈底(bottom),每每次删除的数据元素总是最后插入的数据元素,所以栈又称为“后入先出表”。
特点:后进先出,LIFO(比如推箱子)
栈的储存结构有2种:一种顺序储存结构(顺序栈),一种链式储存结构(链式栈)。

头文件:#include <stack>
1.定义:stack<type>name;
2.入栈在栈顶增加元素 name.push(x)
3.出栈移除栈顶元素,并不返回该元素 name.pop(),
4.取栈顶元素 name.top()
5.判断栈空,当栈空时为真 name.empty(),
6.访问栈中的元素个数 name.size()

例题 括号匹配

Description

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Correct parentheses sequences can be defined recursively as follows:
1.The empty string “” is a correct sequence.
2.If “X” and “Y” are correct sequences, then “XY” (the concatenation of X and Y) is a correct sequence.
3.If “X” is a correct sequence, then “(X)” is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include “”, “()”, “()()()”, “(()())”, and “(((())))”.

Now Yuta has a parentheses sequence S, and he wants Rikka to choose two different position i,j and swap Si,Sj.

Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.

It is too difficult for Rikka. Can you help her?

Input

The first line contains a number t(1<=t<=1000), the number of the testcases. And there are no more then 10 testcases with n>100

For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.

Output

For each testcase, print “Yes” or “No” in a line.

Sample Input

3
4
())(
4
()()
6
)))(((

Sample Output

Yes
Yes
No

Hint

For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.

想法:括号匹配啊,一个个的出来进去用stack

#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)//t个测试组
    {
        stack<char>s;
        int n;
        scanf("%d",&n);
        char str[100005];
        scanf("%s",str);//输入一系列括号用数组存储
        char x,y;
        if(n%2==1)
            printf("No\n");
        else if(n==2)
        {
            x=str[0];
            y=str[1];
            if( x==')' && y=='(' ) printf("Yes\n");
            else printf("No\n");
        }
        else
        {
            char ch;
            while(!s.empty())//如果不为空
                s.pop();//从栈顶弹出
            int i=0;
            while(i<n)
            {
                ch=str[i++];
                if(s.empty())//如果为空
                    s.push(ch);//弹出栈底
                else if(ch==')'&&s.top()=='(')//如果栈顶和栈底相等
                    s.pop();//栈顶弹出
                else
                    s.push(ch);
            }

            if(s.empty())
                printf("Yes\n");//如果空了,说明一一对应了
            else
            {
                if(s.size()==2)
                {
                    x=s.top();
                    s.pop();
                    y=s.top();
                    if( x=='(' && y==')' ) printf("Yes\n");//两个,一个栈顶一个栈底,配对,弹出来
                    else printf("No\n");
                }
                else if(s.size()==4)
                {
                    int a=s.top();s.pop();
                    int b=s.top();s.pop();
                    int c=s.top();s.pop();
                    int d=s.top();s.pop();
                    if(d==')'&&c==')'&&b=='('&&a=='(')
                        printf("Yes\n");//四个,一一配对,弹出来
                    else printf("No\n");//必须要交换顺序配对才算成功
                }
                else printf("No\n");
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值