From 69cc985a7c45fb41a901c26b2868d481f8b3e206 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:40:37 +0800 Subject: [PATCH] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index 177c3878..3245d948 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -681,6 +681,32 @@ impl Solution { } } ``` +### C# +```C# +// 前缀表不减一 +public bool RepeatedSubstringPattern(string s) +{ + if (s.Length == 0) + return false; + int[] next = GetNext(s); + int len = s.Length; + if (next[len - 1] != 0 && len % (len - next[len - 1]) == 0) return true; + return false; +} +public int[] GetNext(string s) +{ + int[] next = Enumerable.Repeat(0, s.Length).ToArray(); + for (int i = 1, j = 0; i < s.Length; i++) + { + while (j > 0 && s[i] != s[j]) + j = next[j - 1]; + if (s[i] == s[j]) + j++; + next[i] = j; + } + return next; +} +```