检测一系列字符何时重复,其时间复杂度优于 O(n!)

发布于 2024-11-02 14:32:39 字数 57 浏览 1 评论 0原文

我需要找到一系列字符(实际上是一个很长的数字)何时开始重复。我认为模式是最简单的。有人可以帮助我吗?

I need to find when a series of characters (it's actually a really long number) begins to repeat itself. I figured that a pattern would be easiest. Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下壹個目標 2024-11-09 14:32:39

如果是数字,则从末尾开始。

找到最后一个 n 和倒数第二个 n 数字相同且重复最大位数的序列。 O(n)

重复序列停止的地方(从末尾开始)就是重复开始的地方。

例如,假设您有 1.2340111101111

您可以看到 1 重复,但仅限 4 位数字。 01111 重复 10 位数字,表示在 1.234 之后开始重复

If its a number, start at the end.

Find the sequence for which the last n and the second last n digit are the same which is repeated over the largest number of digits. O(n)

Where the repeated sequence stops (coming from the end) that is where the repeating starts.

e.g. say you have 1.2340111101111

You can see 1 repeats, but only for 4 digits. 01111 repeats for 10 digits meaning the repeating starts after 1.234

苏璃陌 2024-11-09 14:32:39

根据此序列的来源,这可能有用

Depending on where this sequence is coming from, this might be useful.

找个人就嫁了吧 2024-11-09 14:32:39

我不懂Java。我不确定你的确切问题,但似乎你正在尝试找到序列的最大轨道。下面的伪代码应该会有所帮助:

Given sequence M of length N
Initialize list of indices K
foreach index i from N-1 to N/2
    seqLength := N-i
    isOrbit? := true
    foreach index s from 0 to seqLength-1
        if M[N-1-s] != M[N-1-seqLength-s] then
            isOrbit? := false
    if isOrbit? then
       append(K, i)

当然,最长的轨道是添加到 K 的最后一个条目。该算法约为 O(n^2/4) 左右。本质上,它是一种改进的子序列搜索算法。

I don't know Java. I'm not sure of your exact question but it seems like you are trying to find the maximal orbit of a sequence. The following pseudocode should help:

Given sequence M of length N
Initialize list of indices K
foreach index i from N-1 to N/2
    seqLength := N-i
    isOrbit? := true
    foreach index s from 0 to seqLength-1
        if M[N-1-s] != M[N-1-seqLength-s] then
            isOrbit? := false
    if isOrbit? then
       append(K, i)

The longest orbit is, of course, the last entry added to K. The algorithm is O(n^2/4) or so. It is, essentially, a modified subsequence-search algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文