这些子数组包含哪些元素,能否一一列举?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1047个文字,预计阅读时间需要5分钟。
javapackage test;
import java.util.*;
class MathAlgorithms { /** * 找到已知字符串中出现次数最多的子串 * 首先找到字符串中出现次数最多的1位或2位子串 */ public String findMaxFrequencySubstring(String str) { Map frequencyMap=new HashMap(); int maxFrequency=0; String maxSubstring=;
for (int i=0; i if (frequency > maxFrequency) { maxFrequency=frequency; maxSubstring=subStr; } } } return maxSubstring; }} package test;
import java.util.*;
/**
* @author deity
* 17-4-21 下午6:26
*/
class MathAlgorithms {
/**
* 找到已知字符串中出现最多次的子串
* 首先找到字符串中出现最多次的1、2位的子串,
* 判断这些子串的2、3位是否和1、2位出现次数一样多,以此类推
*
* @param str 已知字符串
*/
void mostSubString(String str) {
String mostStr = "";
int[] p1 = new int[str.length() / 2 + 1];
int max = 1;
for (int j = 0; j < str.length() - 1; j++) {
int[] p2 = new int[str.length() / 2 + 1];
int k = 0;
p2[k++] = j;
String sub = str.substring(j, j + 2);
int count = 1;
for (int i = j + 2; i < str.length() - 1; i++) {
if (sub.equals(str.substring(i, i + sub.length()))) {
count++;
p2[k++] = i;
i++;
}
}
if (max < count) {
max = count;
int i = 0;
while (i < count) {
p1[i] = p2[i];
i++;
}
mostStr = sub;
} else if (max == count) {
boolean isMost = true;
for (int i = 0; i < count; i++)
if (!(p1[i] == (p2[i] - mostStr.length() + 1))) isMost = false;
if (isMost) mostStr = str.substring(p1[0], p1[0] + mostStr.length() + 1);
}
}
System.out.println(mostStr + "*" + max);
}
/**
* 找出两个字符串中相同的子串
*
* @param str1 字符串1
* @param str2 字符串2
*/
List
本文共计1047个文字,预计阅读时间需要5分钟。
javapackage test;
import java.util.*;
class MathAlgorithms { /** * 找到已知字符串中出现次数最多的子串 * 首先找到字符串中出现次数最多的1位或2位子串 */ public String findMaxFrequencySubstring(String str) { Map frequencyMap=new HashMap(); int maxFrequency=0; String maxSubstring=;
for (int i=0; i if (frequency > maxFrequency) { maxFrequency=frequency; maxSubstring=subStr; } } } return maxSubstring; }} package test;
import java.util.*;
/**
* @author deity
* 17-4-21 下午6:26
*/
class MathAlgorithms {
/**
* 找到已知字符串中出现最多次的子串
* 首先找到字符串中出现最多次的1、2位的子串,
* 判断这些子串的2、3位是否和1、2位出现次数一样多,以此类推
*
* @param str 已知字符串
*/
void mostSubString(String str) {
String mostStr = "";
int[] p1 = new int[str.length() / 2 + 1];
int max = 1;
for (int j = 0; j < str.length() - 1; j++) {
int[] p2 = new int[str.length() / 2 + 1];
int k = 0;
p2[k++] = j;
String sub = str.substring(j, j + 2);
int count = 1;
for (int i = j + 2; i < str.length() - 1; i++) {
if (sub.equals(str.substring(i, i + sub.length()))) {
count++;
p2[k++] = i;
i++;
}
}
if (max < count) {
max = count;
int i = 0;
while (i < count) {
p1[i] = p2[i];
i++;
}
mostStr = sub;
} else if (max == count) {
boolean isMost = true;
for (int i = 0; i < count; i++)
if (!(p1[i] == (p2[i] - mostStr.length() + 1))) isMost = false;
if (isMost) mostStr = str.substring(p1[0], p1[0] + mostStr.length() + 1);
}
}
System.out.println(mostStr + "*" + max);
}
/**
* 找出两个字符串中相同的子串
*
* @param str1 字符串1
* @param str2 字符串2
*/
List

