Problem Link - http://www.codechef.com/problems/ONP
My program runs for all inputs I tried, but still gives wrong answer.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<ctype.h>
using namespace std;
int transform(char a[401])
{
int n = strlen(a),i,j,k,l,m,f;
for(int o=1;;o++)
{
f=0;
for(i=n-1;i>=0;i--)
{
if(a[i]=='(')
{
a[i]=-1;
f=1;
break;
}
}
if (f==0)
{
break;
}
for(j=i+1;;j++)
{
if(isalpha(a[j])==0&&a[j]!=-1)
break;
}
for(k=j;;k++)
{
if(isalpha(a[k])!=0)
{
break;
}
}
while((a[k]==-1)||(isalpha(a[k])!=0)||(a[k]=='(')||(a[k]==')'))
{
k--;
}
for(l=k;;l++)
{
if(a[l]==')')
{
a[l]=a[k];
a[k]=-1;
break;
}
}
}
for(m=0;m<n;m++)
{
if((isalpha(a[m])!=0)||a[m]=='+'||a[m]=='-'||a[m]=='*'||a[m]=='/'||a[m]=='^')
{
cout<<a[m];
}
}
return 0;
}
int main()
{
char a[101][401];
int times,q;
cin>>times;
for(q=1;q<=times;q++)
{
gets(a[q]);
transform(a[q]);
}
return 0;
}