找到最长的重复字符串,并将其重复给定的字符串中的次数

编程入门 行业动态 更新时间:2024-10-26 04:23:39
本文介绍了找到最长的重复字符串,并将其重复给定的字符串中的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

例如,指定字符串 ABC FGHI BC KL ABCD LKM ABCDEFG ,该函数将返回字符串 ABCD 和2计数

For example, given string "abc fghi bc kl abcd lkm abcdefg", the function should return string "abcd" and the count of 2.

AO(N ^ 2)解决方案似乎很容易,但我在寻找一个更好的解决方案。

A O(n^2) solution seems easy but I am looking for a better solution.

编辑:如果没有更好的比为O(n ^ 2)有可能比它的方法是最佳的性能明智

Edited: If nothing better than O(n^2) is possible than which approach would be best performance wise.

推荐答案

您可以在线性时间内通过构建的后缀树,并采取从根到最深的内部节点的路径;这会给你时间最长重复的字符串。一旦你有一个字符串,它是微不足道的计算中出现。

You can solve this in linear time by building a suffix tree and taking a path from the root to the deepest internal node; this will give you the longest repeated string. Once you have that string, it's trivial to count the number of times it appears.

更多推荐

找到最长的重复字符串,并将其重复给定的字符串中的次数

本文发布于:2023-11-30 01:08:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   最长   次数   并将其

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!