查找长度为 k 的第一个重复子串的算法
我有一项作业需要做并且需要帮助。我应该编写一个程序来查找长度为 k 的第一个子字符串,该子字符串在字符串中至少重复两次。
例如,在字符串“banana”中,有两个长度为2的重复子字符串:“an”、“na”。在这种情况下,答案是“an”,因为它比“na”出现得早
请注意,简单的 O(n^2) 算法没有用,因为程序的执行时间有时间限制,所以我想它应该是线性时间的。
还有一个提示:使用哈希表。
我不想要代码。我只是想让你给我一个线索,因为我不知道如何使用哈希表来做到这一点。我也应该使用特定的数据结构吗?
There is a homework I should do and I need help. I should write a program to find the first substring of length k that is repeated in the string at least twice.
For example in the string "banana" there are two repeated substrings of length 2: "an" , "na". In this case, the answer is "an" because it appeared sooner than "na"
Note that the simple O(n^2) algorithm is not useful since there is time limit on execution time of program so I guess it should be in linear time.
There is a hint too: Use Hash table.
I don't want the code. I just want you to give me a clue because I have no idea how to do this using a hash table. Should I use a specific data structure too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
迭代字符串的字符索引 (0, 1, 2, ...),直到并包括倒数第二个字符的索引(即直到 strlen(str) - 2)。对于每次迭代,执行以下操作...
提取从字符索引开始的 2 字符子字符串。
检查您的哈希表是否包含 2 个字符的子字符串。如果是的话,你就得到了答案。
将每个 2 个字符的子字符串插入哈希表中。
这很容易修改以处理长度为 k 的子串。
Iterate over the character indexes of the string (0, 1, 2, ...) up to and including the index of the second-from-last character (i.e. up to strlen(str) - 2). For each iteration, do the following...
Extract the 2-char substring starting at the character index.
Check whether your hashtable contains the 2-char substring. If it does, you've got your answer.
Insert each 2-char substring into the hashtable.
This is easily modifiable to cope with substrings of length k.
将 Will A 的算法与滚动哈希相结合,得到线性时间算法。
Combine Will A's algorithm with a rolling hash to get a linear-time algorithm.
您可以使用链接哈希图。
You can use linked hash map.