栈(括号匹配,海豚对视,大鱼吃小鱼)

Speed down, Colombooo!!!
rowing coach, Gabi
As common sense tells us, competitive programmers excel at rowing. The olympic lane is a wonderful place to row, run and work out. What few take their time to appreciate are the capybaras that inhabit the region. Capybaras are fascinating animals! Aside from their beauty, they possess many interesting behaviours. Did you know that capybaras can live in packs as big as 100 individuals?

In a pleasant sunny morning, Yan was running, as usual. Watching the capybaras, he noticed that they would line up to sunbath. Each capybara was paired with another one, and only another one. Two capybaras can be paired if and only if both see each other. A capybara sees everything in the direction it is looking.

Curious, Yan decided to represent the capybaras by the letters A and B, where A indicates that the capybara is looking right, and B indicates that the capybara is looking left.

For example, the sequence AABABB accurately represents capybaras sunbathing, because it is possible to pair every capybara according to the rules above. Yan was so fascinate by this that he slipped and felt into the water, messing his representations. He was able to recover some, but now they are all messed up with each other. Can you help him and find out if a given sequence represent capybaras sunbathing?

Input
Every instance contains a sequence S of characters, composed only of ‘A’ and ‘B’ – Yan’s representation. You may assume that 1 ≤ |S| ≤ 105.

Output
The output should contain a single line. Print “Sim” if the sequence represents capybaras sunbathing, or “Nao” otherwise.

Example
Input
AABABB
Output
Sim
思路:
这个题其实就是括号匹配类型的题,如果是A就代表是左括号,如果是B就代表是右括号,这种配对的题一般就是用栈,刚开始自己的思路是不用栈,直接操作字符串:先匹配两端的,然后中间的就是验证一下是不是AB,很显然这是不对滴,举个反例,AAABBB就可以证明了;用栈的话用得注意BA就不行因为这时候B是向左看而A是向右看的相当于“)(”这种情况;
遇到A就进栈,遇到B判断当前的top是不是A,不是的话退出,当前如果栈是空也退出;如果top是A就top–.最后看一下栈是不是空的,不是空的也不对;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
char s[100001];
using namespace std;
queue<char>q;
int main()
{
    scanf("%s",s);
    int len=strlen(s);
    int f=0;
    for(int i=0;i<len;i++)
    {
        if(s[i]=='A')
        {
            q.push(s[i]);
        }
        else if(s[i]=='B')
        {
            if(q.size()==0||q.front()!='A')
            {
                f=1;
                break;
            }
            else q.pop();
        }
    }
    if(q.size()!=0)f=1;
    if(f==1)printf("Nao\n");
    else printf("Sim\n");
    return 0;
}

数据结构实验之栈与队列四:括号匹配
Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description
给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的( ) ,[ ],{ }是否匹配。

Input
输入数据有多组,处理到文件结束。

Output
如果匹配就输出“yes”,不匹配输出“no”

Sample Input
sin(20+10)
{[}]
Sample Output
yes
no
Hint
Source
ma6174
思路:
如果是左括号就进栈,如果是右括号,得判断当前的栈顶元素是不是相应的左括号,不是就退出,是的话就出栈,再者是看看栈是不是空的,空的得退出;最后总的判断;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
char s[100];
using namespace std;
stack<char>q;
int main()
{
    while(gets(s)!=NULL)//可能有空格,不能用%s
    {
        int len=strlen(s);
        int f=0;
        for(int i=0; i<len; i++)
        {
            if(s[i]=='{'||s[i]=='('||s[i]=='[')
            {
                q.push(s[i]);
//                printf("top==%c\n",q.top());
            }
            else if(s[i]=='}'||s[i]==')'||s[i]==']')
            {
                if(q.size()==0)
                {
                    f=1;
                    break;
                }
//                printf("%c111\n",q.top());
                if((s[i]=='}'&&q.top()!='{')||(s[i]==']'&&q.top()!='[')||
                        (s[i]==')'&&q.top()!='('))
                {
                    f=1;
                    break;
                }
                else if((s[i]=='}'&&q.top()=='{')||(s[i]==']'&&q.top()=='[')||
                        (s[i]==')'&&q.top()=='('))
                {
                    q.pop();
                }
            }
        }
        if(q.size()!=0)f=1;
        if(f==0)printf("yes\n");
        else printf("no\n");
        while(!q.empty())q.pop();
    }
    return 0;
}

F - 大鱼吃小鱼(20)
有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右。游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼。从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右)。问足够长的时间之后,能剩下多少条鱼?
Input
第1行:1个数N,表示鱼的数量(1 <= N <= 100000)。
第2 - N + 1行:每行两个数Ai, Bi,中间用空格分隔,分别表示鱼的大小及游动的方向(1 <= Ai <= 10^9,Bi = 0 或 1,0表示向左,1表示向右)。
Output
输出1个数,表示最终剩下的鱼的数量。
Sample Input
5
4 0
3 1
2 0
1 0
5 0
Sample Output
2

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;
stack<int>q;
int main()
{
    int n,num;
    scanf("%d",&n);
    num=n;
    for(int i=1;i<=n;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        if(b==1)q.push(a);//只要是向右的就入栈;
        else
        {
            while(!q.empty())
            {
                if(q.top()<a)//与向左的进行比较,如果向左的很大,可以把栈里面的鱼都吃了,那就都吃了吧
                //吃光了比较下一个,就算下一个来的是一个向右的,无所谓,因为“向左,向右”他们两个是不会相遇的;
                {
                    q.pop();
                    num--;//如果向左的小,那就被栈顶的鱼给吃了,总数-1,继续比较下一个;
                }
                else
                {
                    num--;
                    break;
                }
            }
        }
    }
    printf("%d\n",num);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值