-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cutting Binary String
67 lines (57 loc) · 1.67 KB
/
Cutting Binary String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class GFG{
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0){
String s = read.readLine();
Solution ob = new Solution();
System.out.println(ob.cuts(s));
}
}
}
// } Driver Code Ends
class Solution{
//User function Template for Java
static boolean hai(String s,int start,int end){
long num = 0,j=0,temp=1;
for(int i=end;i>=start;i--){
if(s.charAt(i)=='1'){
num+=temp;
}
temp*=2;
}
while(num>1){
if(num%5!=0)return false;
num/=5;
}
return true;
}
static int help(String s,int start,int end,int dp[][]){
if(s.charAt(start)=='0')return -1;
if(end<start)return 0;
if(dp[start][end]!=-1)return dp[start][end];
if(hai(s,start,end)==true)return 1;
int ans = Integer.MAX_VALUE;
for(int i=start;i<=end;i++){
if(hai(s,start,i)==true){
int second = help(s,i+1,end,dp);
if(second!=-1)
ans = Math.min(ans,1+second);
}
}
return dp[start][end] = ans==Integer.MAX_VALUE?-1:ans;
}
static int cuts(String s)
{
// code here
int n = s.length();
int dp[][] = new int[n][n];
for(int temp[]:dp)
Arrays.fill(temp,-1);
return help(s,0,n-1,dp);
}
}