1668、最大重复子字符串(暴力+KMP+strstr)

给你一个字符串 sequence ,如果字符串 word 连续重复 k 次形成的字符串是 sequence 的一个子字符串,那么单词 word 的 重复值为 k 。单词 word 的 最大重复值 是单词 word 在 sequence 中最大的重复值。如果 word 不是 sequence 的子串,那么重复值 k 为 0 。

给你一个字符串 sequence 和 word ,请你返回 最大重复值 k 。

示例 1:

输入:sequence = "ababc", word = "ab"
输出:2
解释:"abab" 是 "ababc" 的子字符串。

示例 2:

输入:sequence = "ababc", word = "ba"
输出:1
解释:"ba" 是 "ababc" 的子字符串,但 "baba" 不是 "ababc" 的子字符串。

示例 3:

输入:sequence = "ababc", word = "ac"
输出:0
解释:"ac" 不是 "ababc" 的子字符串。

一、简单枚举

利用for循环,若本轮循环sequence起始字符不等于word[0],则跳过本次循环;

若出现第一个匹配的字符串后,将j=0,继续向下匹配,记录连续匹配的个数,通过比较,得到最大连续匹配个数。

int maxRepeating(char * sequence, char * word){
   int num=0;
   int l1=strlen(sequence);
   int l2=strlen(word);
   for(int i=0;i<l1;i++)
   {
       if(sequence[i]!=word[0])
       continue;
       int k=i;
       int j=0;
       int sum=0;
       while(k<l1&&sequence[k]==word[j])
       {
           k++;
           j++;
           if(j==l2)
           {
               j=0;
               sum++;
           }
       }
       if (num<sum)
       {
           num=sum;
       }
   }
    
   return num;
}

二、KMP算法

kmp算法:查找主串中是否含有子串。

计算最大的word重复次数,利用while进行循环,用KMP判断是否为子串,每次都将word拼接到子串的尾部,若满足条件count++,记录最大满足的重复次数。

class Solution {
public:
    int* getNext(string word)
    {
        int wordLen=word.size();
        int *next = new int[wordLen];
        next[0]=-1;
        int i=0,k=-1;
        while(i<wordLen-1)
        {
            if(k==-1 || word[i]==word[k])
            {
                i++;
                k++;
                next[i]=k;
            }
            else
            {
                k=next[k];
            }
        }
        return next;
    }
    int KMP(string sequence, string word)
    {
        int i=0,j=0;
        int slen=sequence.size(),wlen=word.size();
        int* next = getNext(word);
        while(i<slen && j<wlen)
        {
            if(j==-1 || sequence[i]==word[j])
            {
                ++i;
                ++j;
            }
            else
            {
                j=next[j];
            }
        }
        if(j==wlen)
            return i-j;
        return -1;
    }
    int maxRepeating(string sequence, string word) 
    {
        int count=0;
        string words=word;
        while(KMP(sequence,words)!=-1)
        {
            count++;
            words+=word;
        }
        return count;
    }
};

三、strcat + strstr

先申请一块内存并置零,内存大小最大为sequence的长度*2;

计算最大的word重复次数,利用while进行循环,每次都使用strcat拼接一次word,并用strstr判断是否为子串,若满足条件ans++,记录最大满足的重复次数。

int maxRepeating(char * sequence, char * word){
    int ans = 0;
    int len_s = strlen(sequence);
    int len_w = strlen(word);
    if (len_w > len_s) {
        return 0;
    }

    char str[2 * len_s + 1];
    memset(str, 0, sizeof(str));
    int len = 0;
    while (len <= len_s) {
        strcat(str, word);
        if (strstr(sequence, str) != NULL) {
            ans++;
            len += len_w;
            continue;
        } else {
            return ans;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值