Header Ads Widget

Neutralisation of charges

 Problem:-

In a parallel universe, there are not just two charges like positive and negative, but there are 26 charges represented by  lower 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 s where each si represents a charge, where 0i|s|1.

You need to output size of final string followed by string after which no neutralizing is possible.

Sample Input
12
aaacccbbcccd
Sample Output
2
ad
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;
}



Post a Comment

0 Comments