|
12鱼币
(1)重复子串
读入一个字符串,求出这个字符串中重复次数最多的子串。例如,'ababab'中子串
'a','b','ab'均重复了三次。当然,'ababab'的子串'aba'相当于重复两次。
问题就是上面的,谢谢啦~
这个?
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- bool search(const char *s, const char *e, char ch)
- {
- while(s != e)
- {
- if(*s == ch)
- return true;
- ++s;
- }
- return false;
- }
- size_t test_str(const char *str)
- {
- size_t max = 0;
- size_t len = strlen(str);
- for(size_t i = 0; i < len; ++i)
- {
- for(size_t j = i + 1; j < len; ++j)
- {
- if(search(str + i, str + j, str[j]))
- {
- if(max < j - i)
- max = j - i;
- break;
- }
- }
- }
- return max;
- }
- int main(void)
- {
- char buf[1024];
- scanf("%s", buf);
- printf("%lu\n", test_str(buf));
- return 0;
- }
复制代码
|
|