有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在一段密文中计算出bigram

试图找出为什么我的bi-gram计数器在数组中没有正确排序,并保持正确的匹配计数。非常感谢您的帮助

例如-->;“DLKJGJFFDLJDDLJLDLFK” 答案-->;DL=4

int cipherTxt_length = cipherTxt.length(); String ch; int count=0; for(char i='a'; i<='z'; i++) { System.out.println("inside first for loop -- count -- "+count); for(char k='a'; k<='z'; k++) { System.out.println("inside first for loop -- count -- "+count); count = 0; for(int j=0; j<cipherTxt_length; j++) { System.out.println("inside first for loop -- count -- "+count); ch=cipherTxt.substring(j,j+2); //extracting characters of the string one by one if(ch.charAt(j)==k && ch.charAt(j+2)==i) //first checking the whole string for 'a', then 'b' and so on count++; //increasing count of those aplhabets which are present in the string } if(count!=0)//printing only those aphabets whose count is not '0' { System.out.println(i+"\t\t"+count);//\t is tabbing method } } } }

共 (1) 个答案

  1. # 1 楼答案

    试试这个

    String s =  "DLKJGJFFDLJDDLJLDLFK";
    Map<String, Integer> map = IntStream.range(0, s.length() - 1)
        .mapToObj(i -> s.substring(i, i + 2))
        .collect(HashMap::new, (m, e) -> m.compute(e, (k, v) -> v == null ? 1 : v + 1), (m, n) -> m.putAll(n));
    Entry<String, Integer> max = map.entrySet().stream()
        .max(Comparator.comparing(Entry::getValue)).get();
    System.out.println(max);    // -> DL=4