本文介绍了查找子串最大出现的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个字符串S,找到重复次数最多的子字符串的最佳算法是什么.

Given a string S, what is the best algorithm to find a substring which repeats maximum number of times.

例如,在"assdssfssd"中,"ss"重复最大次数.

For example, in "assdssfssd", it is "ss" which repeats maximum number of times.

推荐答案

我可以看到构建一棵树来解决该特定问题.

I can see building a tree to solve that particular problem.

有一个名义根节点.第一个角色是第一个孩子.在您的情况下,第二个字符是第一个字符a-> s的子代.它还开始了根节点的新叶.如果在添加节点时访问现有节点,则增加其计数(初始值1).

There is a notional root node. The first character is the first child. The second character is a child of the first character a -> s in your case. It also begins a new leaf of the root node. If, in adding a node, you visit an existing node, you increment its count (initial value 1).

完成后,您访问树的每个节点以在最深的级别找到计数最高的节点(因为如果"asdf"出现5次,则"a","as"和"asd"最少出现一次) 5次,根据定义).

Once done, you visit every node of the tree to find the one with the highest count at the deepest level (because if "asdf" occurs 5 times then "a", "as" and "asd" occur a minimum of 5 times, by definition).

这篇关于查找子串最大出现的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:16