创作目的:为了方便自己后续复习重点,以及养成写博客的习惯。
一、串匹配下标
思路:熟悉KMP算法的思路,也查看了ledcode的题解。
ledcode题目:https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/
AC代码:
//前缀表不减一版本
void getNext(int* next, char* s) {
//初始化 next
int j = 0;
next[0] = j;
for (int i = 1; i < strlen(s); i++) { //注意 i 从 1 开始
//若前后缀不相同
while (j > 0 && s[i] != s[j]) {
//则向前回退
j = next[j - 1];
}
//若前后缀相同
if (s[i] == s[j]) {
//i 和 j 同时向后移动(i 的增加在 for 循环里)
j++;
}
//将 j(前缀的长度)赋值给 next[i]
next[i] = j;
}
}
int strStr(char * haystack, char * needle){
int len1 = strlen(haystack);
int len2 = strlen(needle);
//当 needle 为空字符串时,返回 0
if (len2 == 0) {
return 0;
}
//构建 next 数组
int* next = malloc(sizeof(int) * len2);
getNext(next, needle);
//next 记录的起始位置为 0,所以这里也从 0 开始
int j = 0;
for (int i = 0; i < len1; i++) { //注意匹配时 i 从 0 开始
//若不匹配
while (j > 0 && haystack[i] != needle[j]) {
//j 退回到之前匹配的位置
j = next[j - 1];
}
//若匹配
if (haystack[i] == needle[j]) {
//i 和 j 同时向后移动(i 的增加在 for 循环里)
j++;
}
//当 j 等于 needle 的长度时,说明字符串 haystack 里出现了字符串 needle
if (j == len2) {
//返回 needle 字符串出现的第一个位置
return (i - len2 + 1);
}
}
//若未找到则说明不存在,返回 -1
return -1;
}
二、重复的子串
思路:了解KMP后,查看ledcode题解
lecode题目:https://leetcode.cn/problems/repeated-substring-pattern/
AC代码:
void CalcNext(char *p,int *next)
{
next[0]=-1;
int nlen=strlen(p);
int k=-1;
int j=0;
while(j<nlen)
{
if(k==-1||p[j]==p[k])
{
++k;++j;
next[j]=k;
}
else
k=next[k];
}
}
bool repeatedSubstringPattern(char * s){
int length=strlen(s);
int *next=(int *)malloc(sizeof(int)*(length+1));
CalcNext(s,next);
int k=next[length];
int p=length-k;
if(length%p==0&&length!=p)
return true;
return false;
}
全篇后记:
一刷总是会遗忘,加上时间确实不足,能做到紧跟进度,坚持到最后也许会有不一样的结果。