未通过全部测试用例
就像切一块水果蛋糕,字典相当于这块蛋糕上至少包含的水果的组合,问你最多能够切几块?
public static void main(String[] args) {
String s = "abafab";System.out.println(s.length());
String[] dics ={"ab","f","a"};
int n = 3;
getMaxSegs(s,dics,n);
}
public static int getMaxSegs(String S,String[] dics,int n){
int count = 0;
HashMap<String,Integer> map = new HashMap<String,Integer>();
for(int i = 0; i < n; i++){
map.put(dics[i], 1);
}
boolean flag = false;
for(int j = 0; j < S.length();){
System.out.println("====================================");
int m = 1;
String key = "";
while((j+m <= S.length()) && !flag){
key = S.substring(j, j+m);
System.out.println("key:"+key);
if(map.containsKey(key)){
flag = true;
count++;
System.out.println("************************true");
}else{
m++;
System.out.println("false m:"+m);
System.out.println("false m+j:"+(m+j));
}
}
if(flag){
System.out.println("m:"+(m+j));
j = m+j;
System.out.println("j:"+j);
}else{
j++;
}
flag = false;
}
System.out.println("count:"+count);
return count;
}