楼主,我给你 写了一个,不知道你能不能用,你试试吧
import java.util.HashMap;
import java.util.Map;
public class test4 {
public static void main(String[] args) {
String str1 = "abcdefg";//较长的串
String str2 = "acdec";//较短的串
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < str2.length(); i++) {
for (int j = i + 1; j <= str2.length(); j++) {
String key = str2.substring(i, j);
System.out.println(key);
if (str1.contains(key)) {
if (map.containsKey(key)) {
System.out.println(key);
map.put(key, map.get(key) + 1);
} else {
System.out.println(key);
map.put(key, 1);
}
}
}
}
System.out.println("------------------");
int maxValue = 0;
String maxKey = "";
for (Map.Entry<String, Integer> m : map.entrySet()) {
System.out.println("map中key:" + m.getKey() + " value:"
+ m.getValue());
if (m.getKey().length()> maxKey.length()) {
maxValue = m.getValue();
maxKey = m.getKey();
}
}
System.out.println("0000000000000000000");
System.out.println("最大的重复子串为:"+maxKey+" 出现次数为:"+maxValue);
}
} |