字符串中连续出现最多的子串 & 字符串中最长重复子串
2015-08-18 21:20
375 查看
字符串中连续出现最多的子串 & 字符串中最长重复子串
字符串中连续出现最多的子串 & 字符串中最长重复子串,这两个问题都可以用后缀数组来表示,至于后缀数组可以参考编程珠玑P156;后缀数组就是定义一个数组指针,分别指向字符串中的对应位置,如下:a b c a b c a b c d e .substr[0]
b c a b c a b c d e ....substr[1]
c a b c a b c d e .......substr[2]
a b c a b c d e ..........substr[3]
b c a b c d e .............substr[4]
c a b c d e ...............substr[5]
a b c d e .................substr[6]
b c d e ...................substr[7]
c d e .....................substr[8]
d e ........................substr[9]
e ..........................substr[10]
上面的 substr 就是abcabcabcde的后缀数组;
一、字符串中连续出现最多的子串
针对这个问题可以使用后缀数组的思想,可以看到,子串连续出现,则满足 substr[0].substr(i, j - i) = substr[j].substr(0, j - i),知道了这一点程序就好编写了,以下是C++代码:<span style="font-size:18px;">string MaxTimesOfContinue(string str) { int len = str.length(); int maxCount = 0; string longest = ""; for (int i = 0; i < len; ++i) { for (int j = i + 1; j < len; ++j) { if (str.substr(i, j - i) == str.substr(j, j - i)) { int offset = j - i; int count = 2; for (int k = j + offset; j <= len; k += offset) { if (str.substr(i, offset) == str.substr(k, offset)) ++count; else break; } if (count > maxCount) { maxCount = count; longest = str.substr(i, offset); } } } } return longest; }</span>
二、字符串中最长重复子串
这个问题同样可以用后缀数组的思想来做,当然开始肯定想到的是暴力法,即求所有重复子串的长度,之后选择一个最长的即可!
int Comlen(char *str1, char *str2) { int i = 0; while(*str2 && (*str1++ == *str2++)) ++i; return i; } int MaxLength(char *str) { if(str == NULL) return 0; int maxLen = 0; int n = strlen(str); int maxi, maxj; for (int i = 0; i < n; ++i) { for(int j = i + 1; j < n; ++j) { int thisLen = 0; if ((thisLen = Comlen(&str[i], &str[j])) > maxLen) { maxLen = thisLen; maxi = i; maxj = j; } } } return maxLen; }
若是使用后缀数组的方法可以:
对于字符串 banana,其后缀数组为
a[0]:banana
a[1]:anana
a[2]:nana
a[3]:ana
a[4]:na
a[5]:a
将后缀数组按字典排序
a[0]:a
a[1]:ana
a[2]:anana
a[3]:banana
a[4]:na
a[5]:nana
之后比较相邻两个子串即可:
int Comlen(char *str1, char *str2) { int i = 0; while(*str2 && (*str1++ == *str2++)) ++i; return i; } int Pstrcmp(const void *a, const void *b) { return strcmp(*(char**)a, *(char**)b); } //char *a[11]; int MaxLength(char *str) { if(str == NULL) return 0; int maxLen = 0; int len = strlen(str); char **a = new char *[len + 1]; for (int i = 0; i < len ; ++i) a[i] = &str[i]; qsort(a, len , sizeof(char *), Pstrcmp); for (int i = 0; i < len - 1; ++i) if(Comlen(a[i], a[i+1]) > maxLen) maxLen = Comlen(a[i], a[i+1]); return maxLen; }
相关文章推荐
- IOS学习之OC面向对象—封装
- C++ delete错误
- BZOJ 题目1036: [ZJOI2008]树的统计Count(Link Cut Tree,修改点权求两个最大值和最大值)
- Android之如何用代码使编辑框等组件显示为圆角
- hdu-2680Choose the best route (dijkstra求最短路)
- TCP为什么要三次握手而不采用两次握手
- 整形数组合并
- kettle 数据库连接中断重置
- [POJ2369]Permutations
- poj1088 滑雪
- 盒模型
- window打开服务的dos命令
- pat(A)1074. Reversing Linked List(哈希)
- 偏导项不能相消
- 学好英语的18条黄金法则
- 剖析SSH核心原理(一) .
- 算法题1:蛇形打印矩阵
- 【转载】Ubuntu安装Theano+CUDA
- 使用theHarvester 进行邮箱和子域名的收集
- mybatis中使用log4j