


1. Let ‘X’ be a binary number formed by reading a substring of ‘S’ from left to right.
2. ‘X’ must not contain leading zeroes.
3. The decimal equivalent of ‘X’ must be a power of 5.
4. K must be the minimum possible.
If no such ‘K’ possible, then you need to return -1.
The first line of input contains an integer 'T' representing the number of test cases.
The first and the only line of each test case contains a single string, ‘S’.
For each test case, print the minimum possible value of K. If no such ‘K’ possible, then you need to print -1.
The output of each test case will be printed in a separate line.
1 <= T <= 5
1 <= N <= 100
‘S’ contains only characters ‘0’ and ‘1’.
Where ‘T’ is the number of test cases, ‘N’ is the length of the given string.
You do not need to print anything, it has already been taken care of. Just implement the given function.
The idea here is to check all possible partitions. For each index ‘i’ if substring from index ‘i’ to some index ‘j’ is a power of 5 then we will have two options either make a partition from the index or not. So, we will try both of these options and choose the one that gives us the minimum result.
Algorithm:
Description of ‘partition’ function
This function will take two parameters :
int partition(index, S) :
Description of ‘isPowerFive’ function
This function will take one parameter :
bool isPowerFive( S ) :
The idea here is to optimize the previous approach using dynamic programming. We are calling some functions, again and again, so we will call them once and store their values in a ‘dp’ array.
The below diagram shows overlapping subproblems and repetitive calculations of functions. The partition functions marked in orange color are repeating again and again.
Algorithm:
Description of ‘partition’ function
This function will take three parameters :
int partition(index, S, dp) :
Description of ‘isPowerFive’ function
This function will take one parameter :
bool isPowerFive( S ) :
The idea here is to use iterative dynamic programming here. We will calculate the answer from bottom to top.
Algorithm:
Description of ‘isPowerFive’ function
This function will take one parameter :
bool isPowerFive( S ) :