I have tried all the test cases I could think of including those in the comments section. I would be great if someone would suggest the problem here.
Problem Link : http://www.spoj.com/problems/ACODE/
#include<iostream>
#include<vector>
#include<string>
using namespace std;
long cal(int *v,int beg,int end)
{
if(beg>=end)
return 1;
int mid =(beg+end)/2;
long a = cal(v,beg,mid);
long b= cal(v,mid+1,end);
if((v[mid]*10)+v[mid+1] <27)
return (a*b)+(cal(v,beg,mid-1)*cal(v,mid+2,end));
else
return (a*b);
}
int main()
{
string s;
while(1)
{
vector<string> f;
cin>>s;
long sum=0;
int flag=1;
if(s=="0")
break;
int k=s.size();
for(int a=0;a<k;a++)
{
if(s[a]=='0')
{
if(s[a-1]!='1' && s[a-1]!='2')
{
cout<<"0"<<endl;
flag=0;
}
else
{
f.push_back(s.substr(0,a-1));
s=s.substr(a+1,k-(a+1));
k=s.size();
}
}
}
if(flag==0)
continue;
if(f.size()==0)
f.push_back(s);
for(int aa=0;aa<f.size();aa++)
{
string temp=f[aa];
int kk=temp.size();
int v[kk];
for(int a=0;a<kk;a++)
v[a]=temp[a]-'0';
sum=sum+cal(v,0,kk-1);
}
cout<<sum<<endl;
}
}