字符串中的最长重复字串

 

/* 求一个字符串中的最长重复字串 基本思路是利用next数组来实现

 

   next数组的定义 就是 字符串中第J个字符 必有next[J] - 1个重复

 

   字串, 事实上 KMP查找  求模式串next数组  就是指求出模式串J个

 

   字符前 最大的重复子串

 

*/

 

#include <stdio.h>

 

#include <stdlib.h>

 

#include <string.h>

 

#include <malloc.h>

 

 

 

#define __in

 

#define __out

 

#define __in__out

 

#define __DEBUG__XU

 

 

 

typedef struct __MAX_SUB

 

{

 

int start;

 

int len;

 

int tempstart;

 

int templen;

 

}MAX_SUB, *PMAX_SUB;

 

 

 

int GetNext(__out int *next, __in char *substring);

 

 

 

int main(int argc, char **argv)

 

{

 

char *string = "ccccabababab";

 

char *substring;

 

unsigned int len, sublen;

 

int *next;

 

int i;

int flag = 0;

 

#ifdef __DEBUG__XU

 

int j;

 

#endif

 

MAX_SUB maxsub = {0};

 

 

 

len = strlen(string);

 

for(i = 1; i < len; i++)

{

if(string[i] != string[0])

{

flag = 1;

break;

}

}

if(!flag)

{

printf("string has only one sort of char/n");

printf("%s", string);

return 0;

}

for(i = 0; i < len; i++)

 

{

 

#ifdef __DEBUG__XU

 

printf("substring first pos: %d/n", i);

 

#endif

 

substring = string + i;

 

sublen = strlen(substring);

 

next = (int *)malloc(sizeof(int) * (sublen + 1));

 

maxsub.templen = GetNext(next, substring);

 

maxsub.tempstart = i;

 

if(maxsub.templen > maxsub.len)

 

{

 

maxsub.len = maxsub.templen;

 

maxsub.start = maxsub.tempstart;

 

}

 

#ifdef __DEBUG__XU

 

for(j = maxsub.start; j < maxsub.len + maxsub.start; j++)

 

{

 

printf("%c", string[j]);

 

}

 

printf("/n");

 

#endif 

 

free(next);

 

}

 

}

 

 

 

int GetNext(__out int *next, __in char *substring)

 

{

 

char *string;

 

int k, i;

 

int max_k;

 

 

 

string = substring;

 

next[0] = -1;

 

k = next[0];

 

i = 0;

 

max_k = k;

 

while(i < strlen(substring))

 

{

if(i + 1 == strlen(substring) )

{

if(substring[k] == substring[i])

goto kmp;

 

}

 

if(-1 == k || substring[k] == substring[i])

 

{

kmp:

i++;

 

k++;

 

next[i] = k;

 

if (k > max_k)

 

{

 

max_k = k;

 

}

 

}

 

else

 

{

 

k = next[k];

 

}

 

}

 

#ifdef __DEBUG__XU

 

for(i = 0; i < strlen(substring) + 1; i++)

 

{

 

printf("next[%d] = %d  ", i, next[i]);

 

}

 

printf("/n");

 

getchar();

 

#endif

 

return max_k;

 

}

 

在JavaScript中,找出字符串重复最长的子串可以通过多种算法实现,其中一个高效的方法是使用动态规划。下面提供一个基于动态规划的示例实现: ```javascript function longestDupSubstring(S) { const MOD = 1000000007; const base = 26; // 字母的总数 let n = S.length; let maxLen = 0; // 最长相等子串的长度 let startIndex = 0; // 最长相等子串的起始索引 let hash = 0; // 当前子串的哈希值 let basePower = 1; // base的幂,用于计算新的哈希值 // 计算最大的base的n-1次幂 for (let i = 0; i < n; i++) { basePower = (basePower * base) % MOD; } // 计算字符串的哈希值 for (let i = 0; i < n; i++) { hash = (hash * base + S.charCodeAt(i) - 'a'.charCodeAt(0)) % MOD; } // 存储每个字符出现的哈希值和对应的索引 const indexMap = new Map([[hash, 0]]); // 使用滚动哈希来计算子串的哈希值 for (let i = 1; i < n; i++) { hash = (hash * base + S.charCodeAt(i) - 'a'.charCodeAt(0)) % MOD; if (indexMap.has(hash)) { let j = indexMap.get(hash); if (S.slice(j, i) === S.slice(j + maxLen, i + maxLen)) { startIndex = j; maxLen = i - j; } } else { indexMap.set(hash, i); } } // 返回最长重复子串 return S.slice(startIndex, startIndex + maxLen); } // 测试用例 console.log(longestDupSubstring("banana")); // 应该返回 "an" ``` 这段代码首先计算了字符串的哈希值,然后使用一个哈希表来存储出现过的子串哈希值及其起始索引。通过滚动窗口的方法,我们可以在O(n)的时间复杂度内找到最长重复子串。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值