求字符串最大的重复字串
来源:互联网 发布:网校程序源码 编辑:程序博客网 时间:2024/05/17 04:17
1,定义一个后缀数组
2,对后缀数组排序
3,找出最大的重复字串:与其重复的最大字串,一定是相邻的。
private static int partition(String[] src, int low, int high) {if (src == null) {return -1;}String pivotValue = src[low];while (low < high) {while (low < high && src[high].compareTo(pivotValue) > 0)high--;src[low] = src[high];while (low < high && src[low].compareTo(pivotValue) < 0)low++;src[high] = src[low];}src[low] = pivotValue;return low;}public static void quickSort(String[] src, int low, int high) {if (src == null) {return;}if (low < high) {int mid = partition(src, low, high);quickSort(src, low, mid - 1);quickSort(src, mid + 1, high);}}public static String[] getSuffixChars(String src) {if (src == null) {return null;}String[] suffixStrings = new String[src.length()];for (int i = 0; i < src.length(); i++) {suffixStrings[i] = src.substring(i);}return suffixStrings;}private static int commonStringLength(String str1, String str2){if(str1==null || str2==null)return -1;int len1 = str1.length();int len2 = str2.length();int commonLength = 0;for(int i=0; i<len1 && i<len2 ; i++){if(str1.charAt(i) != str2.charAt(i)){break;}commonLength++;}return commonLength;}public static String getMaxRepeatSubString(String src) {// 生成后缀数组String[] suffixStrings = getSuffixChars(src);// 排序后缀数组quickSort(suffixStrings, 0, suffixStrings.length - 1);// 与其重复最多的字串,一定是相邻的两个字串int maxLength = 0;int maxLoc = 0;int commonLength = 0;for(int i=1; i< suffixStrings.length; i++){commonLength = commonStringLength(suffixStrings[i-1], suffixStrings[i]);if(commonLength > maxLength) {maxLength = commonLength;maxLoc = i-1;}}return suffixStrings[maxLoc].substring(0, maxLength);}
0 0
- 求一个字符串的最大重复字串
- 求字符串最大的重复字串
- 求字符串的最大字串
- 求字符串的最长不重复字串
- 求字符串的最长不重复字串
- 求两个字符串的最大公共字串
- 求两个字符串的最大公共字串
- 一个字符串中无重复的字串最大长度
- 求一个字符串的最长重复字串问题
- 求字符串中重复出现的最长字串
- 用后缀数组 求一个字符串的最长重复字串
- 实现用后缀数组求字符串的最长重复字串
- 求字符串最长连续不重复的字串
- 求两个字符串最大的公共字串的长度
- 【题目14】求两个字符串的最大相同字串
- Exe 求两个字符串的最大公共字串
- 求两个字符串的最大公共字串问题
- 求两个字符串的最大连续公共字串
- Windows 驱动开发基础(九)内核函数
- 正则表达式之grep命令
- MySQL错误2003:Can't connect to MySQL server (10060)
- springMVC中遇到的问题
- Asmack之旅(二)packet扩展
- 求字符串最大的重复字串
- VBS基础篇 - 杂项 - 字符集
- UVA - 1404 Prime k-tuple (素数筛选)
- 如何修改android工程的包名
- IIR 滤波器的实现(C++)
- c# 压缩与解压
- 利用poi导出word excel
- 什么是守护进程?
- coco2dx-3.2 MAC版本环境搭配细节