这题的原连接是:http://acm.hdu.edu.cn/showproblem.php?pid=2146Description Everybody knows the number is saved with the binary string in the computer. Now, their have N (1 ≤ N ≤ 1000) binary strings I tell you, your task is tell me what is the most binary substring with K (1 ≤ K ≤ L) characters in the strings I give you. The length of each string is L (1 ≤ L ≤ 60). Output times of the most appearing binary substring.Input Each line will contain three numbers N, L, K.
Following N lines, represent N binary strings.Output One answer one line.Sample Input
2 6 3
101011
110101
2 6 4
101011
110101Sample Output
4
2
-------------------------------------------------------------
怕各位大侠不想看英文,就是说
2 6 3
101011
110101
2是代表有2个二进制字符101011和110101,6代表他们长度各位6,3是子串的长度
要找在二进制字符串中出现次数最多的子串,输出出现次数。
比如
2 6 3
101011
110101 中出现次数最多子串是101,出现了4次,所以输出42 6 4
101011
110101 中出现次数最多子串是1010或0101,都出现了2次,输出2这题的原连接是:http://acm.hdu.edu.cn/showproblem.php?pid=2146
Following N lines, represent N binary strings.Output One answer one line.Sample Input
2 6 3
101011
110101
2 6 4
101011
110101Sample Output
4
2
-------------------------------------------------------------
怕各位大侠不想看英文,就是说
2 6 3
101011
110101
2是代表有2个二进制字符101011和110101,6代表他们长度各位6,3是子串的长度
要找在二进制字符串中出现次数最多的子串,输出出现次数。
比如
2 6 3
101011
110101 中出现次数最多子串是101,出现了4次,所以输出42 6 4
101011
110101 中出现次数最多子串是1010或0101,都出现了2次,输出2这题的原连接是:http://acm.hdu.edu.cn/showproblem.php?pid=2146
是不是应该在二进制上做文章呢?import java.util.Scanner;
public class TheMostBinarySubstring1025 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
int n=scanner.nextInt();
int l=scanner.nextInt();
int k=scanner.nextInt();
String []s=new String[n];
for(int i=0;i<n;i++){
s[i]=scanner.next();
}
String sub[]=subString(s,k,l);
int count=0;
for(int i=0;i<sub.length;i++){
int all=count(sub[i],s);
if(all>count){count=all;}
}
//打印出现最多的次数
System.out.println(count);
}
}
//从字符串数组中,获得所有可能的长度为K的子串,返回子串数组
static String[] subString(String s[],int k,int l){
StringBuffer sb=new StringBuffer();
for(int h=0;h<s.length;h++){
String t=s[h];
for(int i=0;i+k<=l;i++){
String temp=t.substring(i,i+k);
//保证数组中的元素各不相同
if(sb.length()==0||sb.indexOf(temp)==-1){
sb.append(temp+" ");
}
}
}
String sub[]=sb.toString().split(" ");
return sub;
}
//计算String sub在String数组s中出现的次数
static int count(String sub,String s[]){
int count=0;
for(int j=0;j<s.length;j++){
for(int i=0;i+sub.length()<=s[j].length();i++){
if(s[j].indexOf(sub, i)!=-1) {//是否存在于是S[J]中
count++;
i=s[j].indexOf(sub, i);//后移动指针,避免重复运算
}
}
}
System.out.println(sub+"出现"+count);
return count;
}}
一个list
对第一个字符串按长度截取出所有可能的字串放入他,比如长度6,字串长度3,那么最多截出4个。
第二个字符串也这样截出来放入同样的list.....最后就是统计这个list中重复最多的一个为什么。
101011
110101二进制方式的话顺序读101011
a[101]++
a[010]++
a[101]++
a[011]++.
字符串搞个map也一样.最后算map的value最大的数
public static void stat(int[] array) {
if (array == null || array.length == 0) {
return;
}
Arrays.sort(array);
int length = 1;
int element = -1;
for (int i = 1; i < array.length; i++) {
if (array[i] == array[i - length]) {
length++;
element = array[i];
}
}
System.out.println("出现次数最多的元素:" + element);
System.out.println("出现次数:" + length);
}
Arrays.sort(array); array是字符串数组 怎么比较呢?排序的结果是什么
字符串也没关系的,会用默认比较源比较的,这个sort的作用就是把相同的字符串全放一起
比如{str1,str2,str3,str2,str3,str1,str1}
拍序后可能的结果是:
{str1,str1,str1,str2,str2,str3,str3}或者
{str2,str2,str1,str1,str1,str3,str3}... 都没有关系,只要相同记录在一起就可以了
那在问下,hashmap 怎么根据value排序,我知道找value最大的不一定要排序