GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/String/459.Repeated-Substring-Pattern at master · wisdompeak/LeetCode

Expand file tree

/

459.Repeated-Substring-Pattern

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

459.Repeated-Substring-Pattern

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
459.Repeated-Substring-Pattern_v1.cpp
459.Repeated-Substring-Pattern_v1.cpp
 
 
459.Repeated-Substring-Pattern_v2.cpp
459.Repeated-Substring-Pattern_v2.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

Outline

459.Repeated-Substring-Pattern

解法1

暴力尝试所有的循环节长度,查看是否是否每段循环节长度的区间都相等。

解法2

我们计算字符串s的后缀数组suf[i],得到长度len = suf[n-1],这是s最长的前缀字符串使得期恰好也等于s的后缀字符串。

如果我们得到了这样的len,会有什么性质呢?如下图,我们画出最长前缀/后缀字符串的范围(星号)

s: [* * *] [* * *] [* * *] [_____]
      A           B           C
      
s: [_____] [* * *] [* * *] [* * *]
      D           E           F

由最长前缀/后缀字符串的关系,我们知道B的后缀等于F.同时由于B的后缀就是E的后缀,所以得到E的后缀等于F。我们可以类似地一路向前推进。所以只要A+B的长度是F的长度(也就是n-len)的整数倍,那么A+B(或者D+E)就有F作为循环节。因此判断存在循环节的充要条件是len>0 && (n-len)%len==0.

另外,n-len也一定是最小循环节长度。

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.