求一个字符串中连续出现次数最多的子串
2015-09-06 15:47
423 查看
把字符串用后缀树的形式表现出来如下:
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[0].substr(i,j-i) == substr[j].substr(0,j-i),例如上例中的
substr[0].substr(0,3-0) == substr[3].substr(0,3-0)
我们换一种方式来看,不需要生成后缀组,但思想还是一样的。
![](http://images.cnitblog.com/blog/189953/201309/19221050-0f462064b84d4f3e88414565a7c27dab.jpg)
![](http://images.cnitblog.com/blog/189953/201309/19221112-3c4a8c24b6df4082ac72448a2f13531b.jpg)
![](http://images.cnitblog.com/blog/189953/201309/19221124-947b985d618341e9b30533bd09fb446a.jpg)
![](http://images.cnitblog.com/blog/189953/201309/19221131-d2a16177f1bf4af7a65b64a09e914a38.jpg)
代码:
代码中str.substr(pos2,offset)其实相当于后缀组的substr[pos2].substr(0,offset)
把字符串写成后缀组其实相当于站在不同的位置往后看这个数组,所以其实并不需要额外增加存储空间来生成后缀组。
源地址:http://www.cnblogs.com/elaron/p/3330026.html
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[0].substr(i,j-i) == substr[j].substr(0,j-i),例如上例中的
substr[0].substr(0,3-0) == substr[3].substr(0,3-0)
我们换一种方式来看,不需要生成后缀组,但思想还是一样的。
![](http://images.cnitblog.com/blog/189953/201309/19221050-0f462064b84d4f3e88414565a7c27dab.jpg)
![](http://images.cnitblog.com/blog/189953/201309/19221112-3c4a8c24b6df4082ac72448a2f13531b.jpg)
![](http://images.cnitblog.com/blog/189953/201309/19221124-947b985d618341e9b30533bd09fb446a.jpg)
![](http://images.cnitblog.com/blog/189953/201309/19221131-d2a16177f1bf4af7a65b64a09e914a38.jpg)
代码:
代码中str.substr(pos2,offset)其实相当于后缀组的substr[pos2].substr(0,offset)
把字符串写成后缀组其实相当于站在不同的位置往后看这个数组,所以其实并不需要额外增加存储空间来生成后缀组。
#include <iostream> #include <string> using namespace std; void main(){ string str = "abcabcabcccccdefefefefefef"; int len = str.length(); int maxCount = 0; string longest = ""; for(int pos1 = 0; pos1 < len; pos1++) for(int pos2 = pos1 + 1; pos2 < len; pos2++){ if(str.substr(pos1,pos2-pos1) == str.substr(pos2,pos2-pos1)){ int offset = pos2-pos1; int count = 2; for(int k = pos2 + offset; k <= len; k += offset){ if(str.substr(pos1,offset) == str.substr(k,offset)){ count += 1; }else{ break; } } if(count > maxCount){ maxCount = count; longest = str.substr(pos1,offset); } } } cout << longest << "," << maxCount << endl; }
源地址:http://www.cnblogs.com/elaron/p/3330026.html
相关文章推荐
- 详解C语言中的ttyname()函数和isatty()函数的用法
- 51nod 1393:0和1相等串
- Actionbar 切换布局
- 网络负载问题
- 在数组中,插入一个数,并输出。
- centos v7.0解决乱码
- SharePoint 2013 对话框
- HDU 1155 Bungee Jumping(纯物理题)
- 51nod 1393:0和1相等串
- Android初学习 - Fragment解析03
- 酒与污水定律
- 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。
- 理解Fragment生命周期
- [Centos]编译安装apache 2.4
- codeforces 487 A Fight the Monster 暴力枚举
- 关联规则Java实现
- HDU 4414 Finding crosses(搜索)
- Autolayout 描述约束(constraint)语法
- ie8下array的map()方法不支持
- HDU 4414 Finding crosses(搜索)