http://blog.youkuaiyun.com/huangkangying/article/details/6443302
求一个字符串中连续出现次数最多的子串,例如:abcbcbcabc, 这个串中连续出出次数最多的子串是bc, 它出现了3次。
以下是我的实现代码,用c语言实现,已经编译通过。
#include <stdio.h>
#include <string.h>
int count= 0;
char sub_str[256];
void find_str( char *str )
{
int str_len = strlen( str );
int i, j, k;
int temp_cnt = 0;
for( i = 0; i < str_len; i++ )
{
for( j = i + 1; j < str_len; j++ )
{
int n = j - i;
temp_cnt = 1;
if( strncmp( &str[i], &str[j], n ) == 0 )
{
temp_cnt++;
for( k = j +n; k < str_len; k+= n )
{
if( strncmp( &str[i], &str[k], n ) == 0 )
temp_cnt++;
else
break;
}
if( temp_cnt > count )
{
count = temp_cnt;
strncpy( sub_str, &str[i], n );
//memcpy( sub_str, &str[i], n ); // 也比较关键
}
}
}
}
}
int main( void )
{
char *str = "abcbcbcabc";
find_str( str );
printf( "%d, %s\n", count, sub_str);
}
本文介绍了一个使用C语言实现的算法,用于找出给定字符串中连续出现次数最多的子串及其出现次数。示例代码针对字符串abcbcbcabc进行了测试,结果显示连续出现次数最多的子串为bc,共出现了3次。
2236

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



