求一个字符串中连续出现次数最多的子串

求一个字符串中连续出现次数最多的子串,例如:abcbcbcabc, 这个串中连续出出次数最多的子串是bc, 它出现了3次。

以下是我的实现代码,用c语言实现,已经编译通过。

#include 
#include 
#include 

int count = 0
char sub_str[256]; 

void find_str(char *str) 

10     int str_len = strlen(str); 
11     int i, j, k; 
12     int tmp_cnt = 0
13 
14     for (i = 0; i < str_len; i++) 
15     { 
16         for (j = i+1; j < str_len; j++) 
17         { 
18             int n = j-i;                         //sub string length 
19             tmp_cnt = 1
20             if (strncmp(&str[i], &str[j], n) == 0)   //compare n-lengths strings 
21             { 
22                 tmp_cnt++;                          //they are equal, so add count 
23                 for (k = j+n; k < str_len; k += n)  //consecutive checking 
24                 { 
25                     if (strncmp(&str[i], &str[k], n) == 0
26                     { 
27                         tmp_cnt++; 
28                     } 
29                     else 
30                         break
31                 } 
32                 if (count < tmp_cnt) 
33                 { 
34                     count = tmp_cnt; 
35                     memcpy(sub_str, &str[i], n); //record the sub string 
36                 } 
37             } 
38         } 
39 
40     } 
41 
42 
43 int main() 
44 
45     char *str = "abcbcbcabc"
46     find_str(str); 
47     printf("%d%s/n", count, sub_str); 
48     return 0
49 
50 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值