栈之括号匹配类型

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;
}

猜你喜欢

转载自blog.csdn.net/BHliuhan/article/details/81624101