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
这就是个括号配对题,注意下栈的用法
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e7+10;
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
string s;
cin>>s;
int len=s.size();
stack<char> st;
bool flag=0;
for(int i=0;i<len;i++)
{
if(s[i]=='A')
st.push(s[i]);
else if(!st.empty())//一定要判断是否为空,不然会出现错误
{
st.pop();//栈为空时,再执行这个操作程序会崩溃
}
else
{
flag=1;
break;
}
}
if(st.empty()&&!flag) cout<<"Sim"<<endl;
else cout<<"Nao"<<endl;
return 0;
}