#include <stdc++.h>
using namespace std;
typedef long long int ll;
ll toInt(string k)
{
return ((k[0]-'0')*10+(k[1]-'0'));
}
int main()
{
ll f=1;
while(f)
{
string s;
cin>>s;
if(s==" "||s=="0")
f=0;
else
{
ll len = s.length();
ll pc = 1,c=1,t,zf=0;
for(int i=1;i<len;i++)
{
ll k;
if(i+1 < len && s[i+1]=='0' &&(s[i]=='1' || s[i]=='2'))
k=0;
else
k = toInt(s.substr(i-1,2));
//cout<<k<<endl;
if(s[i]>='1' && s[i] <= '9' && k >= 11 && k <= 26)
{
t = c;
c = pc + c;
pc = t;
}
if(s[i]>='1' && s[i] <= '9' && k > 26)
{
pc = c;
//cout<<k<<endl;
}
if(s[i]=='0' && (s[i-1] != '1' && s[i-1]!='2'))
zf = 1;
}
//if(len%2==1&&s[len-1]!='0')
// c++;
if(zf || s[0] == '0')
cout<<0<<endl;
else
cout<<c<<endl;
}
}
return 0;
}
My plan is to use the O(1) space instead of saving the entire DP array. Not, sure which cases I am failing. Will be glad to receive the reviews!
TIA