题目: 求一个字符串中连续出现次数最多的子串,例如:abcbcd 最多的子串为bc
#include <iostream>
#include <string.h>
using namespace std;
char substr[255];
void findmaxsubstr(char *str){
int len=strlen(str);
int count=0;
int maxcount=0;
for (int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++){
int n=j-i;
count=1;
if (strncmp(&str[i],&str[j],n)==0)
{
count++;
for (int k=j+n;k<len;k+=n)
{
if (strncmp(&str[i],&str[k],n))
{
count++;
}
else
break;
}
if (maxcount<count)
{
maxcount=count;
memcpy(substr,&str[i],n);
}
}
}
}
}
本文介绍了一种用于寻找字符串中最长重复子串的算法实现。通过遍历字符串并比较不同位置的子串来确定重复次数最多的部分。该算法利用 C 语言标准库函数进行字符串匹配,最终找到并返回出现次数最多的子串。
2226

被折叠的 条评论
为什么被折叠?



