File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -879,6 +879,52 @@ public int[] GetNext(string s)
879879}
880880```
881881
882+ ### C
883+
884+ ``` c
885+ // 前缀表不减一
886+ int *build_next (char* s, int len) {
887+
888+ int *next = (int *)malloc(len * sizeof(int));
889+ assert(next);
890+
891+ // 初始化前缀表
892+ next[0] = 0;
893+
894+ // 构建前缀表表
895+ int i = 1, j = 0;
896+ while (i < len) {
897+ if (s[i] == s[j]) {
898+ j++;
899+ next[i] = j;
900+ i++;
901+ } else if (j > 0) {
902+ j = next[j - 1];
903+ } else {
904+ next[i] = 0;
905+ i++;
906+ }
907+ }
908+ return next;
909+ }
910+
911+ bool repeatedSubstringPattern(char* s) {
912+
913+ int len = strlen(s);
914+ int *next = build_next(s, len);
915+ bool result = false;
916+
917+ // 检查最小重复片段能否被长度整除
918+ if (next[len - 1]) {
919+ result = len % (len - next[len - 1]) == 0;
920+ }
921+
922+ free(next);
923+ return result;
924+ }
925+
926+ ```
927+
882928
883929<p align="center">
884930<a href="https://programmercarl.com/other/kstar.html" target="_blank">
You can’t perform that action at this time.
0 commit comments