import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); String[] str = new String[3800]; int n = cin.nextInt(); String yuan = cin.next(); int len = yuan.length(); int ct = 0; for(int i = 0; i < len; i++) { for(int j = i; j < len; j++) { // i 和 j 的关系 str[ct++] = yuan.substring(i, j+1); } } int[] cishu = new int[ct]; //哦统计 for(int i = 0; i < ct; i++) { for(int j = 0; j < ct; j++) { if(i != j && str[i].equals(str[j])) { cishu[i]++; } } } int max = 0; String ans = ""; //最长的串 for(int i = 0; i < ct; i++) { if(str[i].length() >= n) { if(cishu[i] > max) { max = cishu[i]; ans = str[i]; } else if(cishu[i] == max) { if(str[i].length() > ans.length()) { max = cishu[i]; ans = str[i]; } } } } if(ans.length() == 0) { } else { System.out.println(ans); } } } //4 //bbaabbaaaaa
原文:https://www.cnblogs.com/zhumengdexiaobai/p/10421957.html