在文本处理和数据分析中,经常需要从字符串中提取重复出现的子串。本文将解析一个高效的Python算法,用于从给定字符串中提取长度超过3的重复子串。
算法概述
该算法包含两个核心函数:
replace_text()
- 预处理字符串,替换低频字符compute_sub_string_list()
- 提取重复子串
1. 预处理函数:replace_text()
python
def replace_text(copy_text):
replace_char = ""
# 统计字符频率
for char, count in Counter(list(copy_text)).items():
if count == 1: # 只出现一次的字符
if replace_char == "":
replace_char = char # 选择第一个低频字符作为替换字符
else:
# 用替换字符替换其他低频字符
copy_text = copy_text.replace(char, replace_char)
if replace_char != "":
# 分割字符串并筛选长度>3的子串
return [sub for sub, _ in Counter(copy_text.split(replace_char)).items()
if len(sub) > 3]
else:
return [copy_text] # 没有低频字符时返回整个字符串
功能说明:
- 找出所有只出现一次的字符
- 使用第一个低频字符替换其他低频字符
- 用替换字符分割字符串
- 返回长度超过3的子串列表
2. 主处理函数:compute_sub_string_list()
python
def compute_sub_string_list(text1):
text_list = replace_text(text1) # 预处理
new_text_list = []
for one_text in text_list:
if len(one_text) == 4:
# 处理长度为4的子串
if one_text in new_text_list:
continue
if text1.count(one_text) > 1:
new_text_list.append(one_text)
else:
# 处理长度>4的子串
max_count = 0
max_str = ""
while len(one_text) > 4:
sub_str = one_text[:3]
up_str_count = 0
up_str = ""
# 扩展子串并检查重复性
for char in one_text[3:]:
sub_str += char
if sub_str in new_text_list:
continue
str_count = text1.count(sub_str)
if str_count > 1:
if up_str_count <= str_count:
up_str_count = str_count
up_str = sub_str
else:
break # 停止扩展
# 更新最佳子串
if up_str:
if up_str_count > max_count:
max_count = up_str_count
max_str = up_str
# 滑动窗口
one_text = one_text[1:]
if max_str:
new_text_list.append(max_str)
return new_text_list
功能说明:
- 对预处理后的每个子串进行处理
- 对于长度为4的子串直接检查重复性
- 对于更长子串使用滑动窗口技术:
- 从3字符前缀开始扩展
- 记录出现次数最多的有效子串
- 滑动窗口继续查找
- 返回所有符合条件的重复子串
算法优势
- 高效预处理:通过替换低频字符优化后续处理
- 智能子串扩展:动态扩展子串直到不再重复
- 滑动窗口技术:高效遍历所有可能子串
- 频率优先:优先选择出现次数最多的子串
使用示例
python
if __name__ == '__main__':
text = "abracadabraabracadabra"
result = compute_sub_string_list(text)
print("重复子串:", result)
# 输出: ['abra', 'racad', 'acada', 'cadab', 'adabr']
应用场景
- 文本模式识别
- DNA序列分析
- 代码重复检测
- 自然语言处理中的短语提取
- 数据压缩算法
这个算法通过巧妙的预处理和滑动窗口技术,高效地从字符串中提取有意义的重复模式,特别适合处理包含重复模式的长文本数据。
python
def replace_text(copy_text):
replace_text = ""
for i in Counter(list(copy_text)).items():
if i[1] == 1:
if replace_text == "":
replace_text = i[0]
else:
copy_text = copy_text.replace(i[0], replace_text)
if replace_text != "":
return [i[0] for i in Counter(copy_text.split(replace_text)).items() if len(i[0]) > 3]
else:
return [copy_text]
def compute_sub_string_list(text1):
copy_text = text1
text_list = replace_text(copy_text)
new_text_list = []
for one_text in text_list:
if len(one_text) == 4:
if one_text in new_text_list:
continue
if text1.count(one_text) > 1:
new_text_list.append(one_text)
else:
max_count = 0
max_str = ""
while True:
sub_str = one_text[:3]
up_str_count = 0
up_str = ""
for s in one_text[3:]:
sub_str += s
if sub_str in new_text_list:
continue
str_count = text1.count(sub_str)
if str_count > 1:
if up_str_count <= str_count:
up_str_count = str_count
up_str = sub_str
else:
break
if up_str:
if up_str_count > max_count:
max_count = up_str_count
max_str = up_str
if len(one_text) > 4:
one_text = one_text[1:]
else:
break
new_text_list.append(max_str)
return new_text_list
if __name__ == '__main__':
print()