Simple cpp code using lower bound and search STL functions, normal PnC logic


#1

int Solution::solve(vector &A, int B, int C)
{
int n=A.size(),orgB=B;
if(n==0)
return 0;
if(B==1)
return (lower_bound(A.begin(),A.end(),C)-A.begin());
stack s;
while(C>0)
{
s.push(C%10);
C/=10;
}
if(s.size()<B)
return 0;
if(s.size()>B)
{
int ans=pow(n,B);
if(A[0]==0)
return ans-pow(n,B-1);
return ans;
}
bool flag=binary_search(A.begin(),A.end(),s.top());
if(flag)
{
int ans=0;
while(!s.empty() && binary_search(A.begin(),A.end(),s.top()))
{
ans+=(lower_bound(A.begin(),A.end(),s.top())-A.begin())(pow(n,B-1));
s.pop();
B–;
}
if(!s.empty())
ans+=(lower_bound(A.begin(),A.end(),s.top())-A.begin())
(pow(n,B-1));
if(A[0]==0)
return ans-(pow(n,(orgB-1)));
return ans;
}
else
{
int ans=(lower_bound(A.begin(),A.end(),s.top())-A.begin())*(pow(n,B-1));
if(A[0]==0)
return ans-(pow(n,(orgB-1)));
return ans;
}
}


#2

use * in all the multiplications. It got edited and removed. lower_bound ***** pow.