如何在无穷数列中找到第一个与子串匹配的位置

2024-09-27 04:28:56 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个无限字符串,它由从1开始的正整数串联而成:

1234567891011121314151617181920212223...

找到给定子序列的第一个匹配位置的有效算法是什么(最好使用Python)?在

例如,对于子序列

^{pr2}$

答案一定是

 4

对于子序列

111 

答案一定是

12

Tags: 字符串答案算法序列正整数pr2定子
1条回答
网友
1楼 · 发布于 2024-09-27 04:28:56

这将完成大部分工作:

For each number in the sequence:
    For each suffix (from longest to shortest) of the number as a string that the subsequence starts with:
        append enough of the following subsequence numbers in the sequence to make a string long enough that the subsequence would fit in it
        if the subsequence is in that string, you are done

您需要跟踪每个序列号在“无限字符串”中的位置,并根据使用的后缀进行调整。在

相关问题 更多 >

    热门问题