G - MaratonIME goes rowing(类似于括号匹配)

G - MaratonIME goes rowing

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 Aindicates 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.

Eaxmple

Input

AABABB

Output

Sim

大概意思就是:存在AB成对出现,必须是先A后B(类似于括号匹配问题)。


代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char s[100010], a[100010]; // 数组模拟栈
    int top, i, l, flag;
    while(~scanf("%s", s))
    {
        l = strlen(s);
        top = 0;
        flag = 0;
        for(i = 0; i < l; i++)
        {
            if(top == 0)
            {
                if(s[i] == 'A')
                {
                    a[++top] = 'A';
                }
                if(i == l - 1 && s[i] == 'A')
                {
                    flag = 1;
                    break;
                }
                else if(s[i] == 'B')
                {
                    flag = 1;
                    break;
                }
            }

            else
            {
                if(s[i] == 'B' && a[top] == 'A')
                {
                    top--;
                }
                else if(s[i] == 'A')
                {
                    if(i == l - 1 && s[i] == 'A')
                    {
                        flag = 1;
                        break;
                    }
                    else
                    {
                        a[++top] = 'A';
                    }
                }
            }
        }
        if(!top && !flag)
        {
            printf("Sim\n");
        }
        else if(flag)
        {
            printf("Nao\n");
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WMYBlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值