Find the substring with the longest repeat times in a string and calculate its repeat times.

Source: Internet
Author: User
Original question

Find the substring with the longest repeat times in a string and calculate its repeat times. For example, the string "ABC fghi bc kl abcd lkm abcdefg" and returns "ABCD" and 2.

My ideas

For convenient expression, we use the variable SRC as the original string and sub_str as the substring.

Because the question requires that the longest substring be repeated at least twice, the focus is on the longest substring, not the maximum number of repetitions. Therefore, we can start with the longest string and calculate its repetition times. The string can be returned as long as it is repeated twice.

Obviously, the longest substring is the original string, and its number of repetitions is 1, which does not meet the requirements. Therefore, our outer loop can start with Len = strlen (SRC) until Len = 1. If the number of repetitions reaches 2, this string is returned. If no repetition exists, returns 0.

To make the main function clearer, I split the entire program into three parts:

int find_longest_dup_str(char* src, char* dest);int str_sub(char* src, char* sub, int pos,int len);int str_cnt(char* src,char* sub);

The function implemented by find_longest_dup_str is to store the longest string that repeats twice or more times into DEST and return its repetition times.

Str_sub implements the function of saving the sub string with the length of Len from position POs in the original SRC string to sub.

Str_cnt calculates the number of repeated sub-strings.

Implementation Code
/*************************************************************************    > File Name: testmain.c    > Author: KrisChou    > Mail:[email protected]     > Created Time: Sun 17 Aug 2014 02:56:28 PM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>int find_longest_dup_str(char* src, char* dest);int str_sub(char* src, char* sub, int pos,int len);int str_cnt(char* src,char* sub);int main(int argc, char* argv[]){    char str[] = "abc fghi bc kl abcd lkm abcdefg";    char sub[128] = "";    int cnt;    cnt = find_longest_dup_str(str,sub);    printf("result: %s\ncnt: %d \n",sub,cnt);}int find_longest_dup_str(char* src, char* dest){    int len;    int index;    int cnt;    for(len = strlen(src);len > 0;len--)    {        for(index = 0; index + len-1 < strlen(src); index++)        {            str_sub(src,dest,index,len);            if((cnt = str_cnt(src,dest)) >= 2)            {                return cnt;            }        }    }    return 0;}int str_sub(char* src, char* sub, int pos,int len){    int index;    for(index = 0; index < len;index++)    {        sub[index] = src[pos + index];    }    sub[index] = ‘\0‘;}int str_cnt(char* src,char* sub){    int cnt = 0;    char tmp[128];    int index;    int index_sub;    for(index = 0;index + strlen(sub)-1 < strlen(src);index++)    {        /* method1        for(index_sub = 0; index_sub < strlen(sub);index_sub++)        {            if(src[index + index_sub] != sub[index_sub])            {                break;            }        }        if(index_sub == strlen(sub))        {            cnt++;        }        */        /* method2        str_sub(src,tmp,index,strlen(sub));        if(strcmp(sub,tmp) == 0)        {            cnt++;        }        */        // method3        if(strncmp(src + index,sub,strlen(sub))== 0)        {            cnt++;        }    }    return cnt;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.