Idea: This problem uses HashMap to save the enumeration string, the key value to save the string-value value of the number of occurrences of the Save string;
Use the For loop and the substring () method to enumerate all the strings that meet the required substring MAXSTR record the most occurrences of the string.
MaxValue record the number of occurrences
1 A substring of a truncated length greater than or equal to L is first compared if there is already a map, 1.1 If present, the number of times it is removed and +1. And the maximum number of times compared 1.1.1 if greater than the maximum number of times, this number is assigned to the maximum number of times, and the substring is assigned to the maximum number of substrings 1.1.2 If equal to the maximum number of times, then determine which of the two substrings of the length of a large length of the maximum number of records of the substring 1.2 The number of substrings and times to be processed, and in map 1.3 If not present, the new string and 0 are stored in the map
1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 ImportJava.util.Scanner;4 Public class_87 Sub-string Statistics {5 Public Static voidMain (string[] args) {6Scanner Scanner =NewScanner (system.in);7 intL=scanner.nextint ();8String s =Scanner.next ();9 //string save substring, int save numberTenmap<string, integer> map =NewHashmap<string,integer>(); One intMaxValue = 0;//maximum number of times AString maxstr = "";//the maximum number of substrings that occur - for(inti = 0; I < s.length ()-l+1; i++) { - for(intj = i+l; J < S.length (); J + +) { theString string = S.substring (i, j);//enumerates a string with a length greater than or equal to L - if(Map.containskey (String)) {//value +1 in map (key,value) If the string was previously present at intValue =Map.get (string); -value++; - if(value>maxValue) { -MaxValue =value; -Maxstr =string; -}Else if(Value = =maxValue) { in if(Maxstr.length () < String.Length ()) {//if the string appears equal to the string with the most occurrences, the length of the two string is compared, and the string length is taken -Maxstr =string; to } + } - map.put (string, value); the}Else{//Add if the string did not appear before, and initialize the value to 0 *Map.put (String, 0); $ }Panax Notoginseng } - } the System.out.println (MAXSTR); + } A}
Java Blue Bridge cup algorithm improves string statistics