Nice Arches
Nikki's latest work is writing an story of letters. However, she finds writing story so boring that, after working for three hours, she realized that all she has written are M long words consisting entirely of letters A and B. Having accepted that she will never finish the story in time, poor Nikki has decided to at least have some fun with it by counting bubbly words.
Now Nikki is connecting pairs of identical letters (A with A, B with B) by drawing arches above the word. A given word is bubbly if each letter can be connected to exactly one other letter in such a way that no two arches intersect. So here is your task. Help Nikki count how many words are bubbly.
Input :
The first line of input contains the positive integer M , the number of words written down by Nikki. Each of the following M lines contains a single word consisting of letters A and B, with length between 2 and 10^5, inclusive. The sum of lengths of all words doesn't exceed 10^6.
Output :
The first and only line of output must contain the number of bubbly words.
Constraints:
1 <= M <= 100
Sample Input
3 ABAB AABB ABBA
Sample Output
2
Explanation
using namespace std;
int main()
{
int n;
cin>>n;
int ans=0;
for(int i=0;i<n;i++)
{
string st;
cin>>st;
int len=st.length();
stack<char> s;
for(int i=0;i<len;i++)
{
if(!s.empty() && s.top()==st[i])
{
s.pop();
}
else
{
s.push(st[i]);
}
}
if(s.empty())
{
ans++;
}
}
cout<<ans<<endl;
}
ABAB - It is not bubbly as A(indexed 1) will connect to A(indexed 3) by an arch and when we try to connect B(indexed 2) with B(indexed 4) by an arch then it will intersect with the arch b/w A and A. AABB - It is bubbly as arch b/w A and A will not intersect with the arch b/w B and B. ABBA - It is also bubbly as arches will not intersect. we can draw arches b/w A and A above the arch b/w B and B.
--------------------------------------------------------------------EDITORIAL------------------------------------------------------------
MAIN OBSERVATION IS THAT A CHAR ALWAYS PAIR UP WITH THE NEAREST CHAR BUT THERE MUST BE NO CHAR B/W THEM UNPAIRED
Let us describe the procedure of determining if a word is bubbly or not. We iterate along the word from left to right and keep the array of letters which we have not been able to pair yet. Assume we have encountered a letter A. We will check if the last unpaired letter is A: if it is, we can pair it with the new letter and remove it from the array of unpaired letters. If the last unpaired letter is B, we must add the new letter A to the array of unpaired letters (we cannot pair it with some previous A because if we try to connect the B afterwards, its arch will intersect with the arch from letter A).
Notice that the array of unpaired letters we keep is a stack: a structure which enables adding elements to the end, having access to the last element and removing the last element. If the stack is empty after we are done iterating, the word is bubbly.
-----------------------------------------------------------------------CODE----------------------------------------------------------------
#include<bits/stdc++.h>using namespace std;
int main()
{
int n;
cin>>n;
int ans=0;
for(int i=0;i<n;i++)
{
string st;
cin>>st;
int len=st.length();
stack<char> s;
for(int i=0;i<len;i++)
{
if(!s.empty() && s.top()==st[i])
{
s.pop();
}
else
{
s.push(st[i]);
}
}
if(s.empty())
{
ans++;
}
}
cout<<ans<<endl;
}
No comments:
Post a Comment