统计出现频率最高的二元字符(两个字符)的组合

String str = “ystemtakesinordertosolveproblemsbypruningthesystemssearchspaceforinstancebutinordertodetermineateachstepwhichrulesareapplicablethesystemmustmatchthemagainstitscurrentsituationusingcurrenttechniquesthematcherslowsdownasmoreandmorerulesareacquiredsoeachsteptakeslongerandlongerthisectcanoutweighthereductioninthenumberofstepstakensothatthenetresultisaslowdownthishasbeenobservedinseveralrecentsystemsminton1988aetzioni1990tambeetal1990cohen1990ofcoursetheproblemofslowdownfromincreasingmatchcostisnotrestrictedtosystemsinwhichthepurposeofrulesistoreducethenumberofproblemsolvingstepsasystemacquiringnewrulesforanypurposecanslowdowniftherulessignicantlyincreasethematchcostandintuitivelyoneexpectsthatthemoreproductionsthereareinasystemthehigherthetotalmatchcostwillbethethesisofthisresearchisthatwecansolvethisprobleminabroadclassofsystemsbyimprovingthematchalgorithmtheyuseinessenceouraimistoenablethescalingupofthenumberofrulesinproductionsystemsweadvancethestateoftheartinproductionmatchalgorithmsdevelopinganimprovedmatchalgorithmwhoseperformancescaleswellonasignicantlybroaderclassofsystemsthanexistingalgorithmsfurthermorewedemonstratethatbyusingthisimprovedmatchalgorithmwecanreduceoravoidtheutilityprobleminalargeclassofmachinelearningsystems”;

    Map<String, Integer> map = new TreeMap<String, Integer>();
    char[] ch = str.toCharArray();
    for (int i = 0; i < ch.length - 1; i++) {
        map.put(ch[i] + "" + ch[i + 1], map.get(ch[i] + "" + ch[i + 1]) == null ? 1 : map.get(ch[i] + "" + ch[i + 1]) + 1);
    }
    List<Map.Entry<String, Integer>> mapList = new ArrayList<Map.Entry<String, Integer>>(
            map.entrySet());
    Collections.sort(mapList, new Comparator<Map.Entry<String, Integer>>() {
        public int compare(Map.Entry<String, Integer> num1,
                           Map.Entry<String, Integer> num2) {
            return num1.getValue().compareTo(num2.getValue());
        }
    });

    System.out.println(map);
    int temp=0;
    String strMax="";
    for (Map.Entry<String, Integer> mapping : mapList) {

        if(temp<mapping.getValue()){
            temp= mapping.getValue();
           strMax= mapping.getKey();
        }

    }
      System.out.print("出现次数最多的"+strMax + ":共出现" +temp +"次数");
      }
}

结果是th 41次

发布了33 篇原创文章 · 获赞 0 · 访问量 844

猜你喜欢

转载自blog.csdn.net/ninth_spring/article/details/104925035