C代码:
这题是求重复的字符串、是字符串整体公共,但是你不能套公共子串的算法!公共子串是“局部 + 整体” 的公共!
C语言 strstr 函数: 用于找到子串(str2)在一个字符串(str1)中第一次出现的位置。
strstr就很好的用在此处解题!因为要查找 str2 是否在 str1 中出现过!
// 思路错误:1、最大重复子字符串,说明要遍历所有的起点找匹配到的最大长度,而不是只有一个起点;
// 2、也不能使用公共子串的那个套路,因为构造重复字符串后,求的就是公共子串!并不是拿着字符串去匹配!
int maxRepeating(char * sequence, char * word){
int res = 0;
int lenS = strlen(sequence);
char buff[101];
buff[0] = '\0';
while(strlen(buff) < lenS) {
strcat(buff, word);
if (strstr(sequence, buff) != NULL) {
++res;
}
}
return res;
}