Problem:-
In a parallel universe, there are not just two charges like positive and negative, but there are charges represented by english alphabets.
Charges have a property of killing each other or in other words neutralizing each other if they are of same charge and next to each other.
You are given a string where each represents a charge, where .
You need to output of final string followed by string after which no neutralizing is possible.
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
aaacccbbcccd -> accd -> ad
Code:-
#include<stdio.h>
char stack[100000];
long int top=-1;
int main()
{
long int n,i;
char str[100027];
scanf("%ld",&n);
scanf("%s",str);
for(i=0;str[i];i++)
{
if(top!=-1 && str[i]==stack[top])
{
top--;
}
else
{
top++;
stack[top]=str[i];
}
}
printf("%ld\n",top+1);
for(i=0;i<=top;i++)
printf("%c",stack[i]);
return 0;
}
0 Comments