棧(括號匹配,海豚對視,大魚喫小魚)

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章