gpt4 book ai didi

algorithm - 我们如何从 LCP 阵列构建 LCP-LR 阵列?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:17:59 26 4
gpt4 key购买 nike

求给定字符串P(长度m)在文本T(长度N)中出现的次数

我们必须对 T 的后缀数组使用二分查找。

使用标准二进制搜索(没有 LCP 信息)的问题是,在您需要进行的每次 O(log N) 比较中,您将 P 与后缀数组的当前条目进行比较,这意味着一个完整的字符串比较最多 m 个字符。所以复杂度为 O(m*log N)。

LCP-LR 阵列有助于将其改进为 O(m+log N)。 know more

我们如何从 LCP 阵列预先计算 LCP-LR 阵列?

LCP-LR 如何帮助找到模式的出现次数?

请举例说明算法

谢谢

最佳答案

// note that arrSize is O(n)
// int arrSize = 2 * 2 ^ (log(N) + 1) + 1; // start from 1

// LCP = new int[N];
// fill the LCP...
// LCP_LR = new int[arrSize];
// memset(LCP_LR, maxValueOfInteger, arrSize);
//

// init: buildLCP_LR(1, 1, N);
// LCP_LR[1] == [1..N]
// LCP_LR[2] == [1..N/2]
// LCP_LR[3] == [N/2+1 .. N]

// rangeI = LCP_LR[i]
// rangeILeft = LCP_LR[2 * i]
// rangeIRight = LCP_LR[2 * i + 1]
// ..etc
void buildLCP_LR(int index, int low, int high)
{
if(low == high)
{
LCP_LR[index] = LCP[low];
return;
}

int mid = (low + high) / 2;

buildLCP_LR(2*index, low, mid);
buildLCP_LR(2*index+1, mid + 1, high);

LCP_LR[index] = min(LCP_LR[2*index], LCP_LR[2*index + 1]);
}

引用:https://stackoverflow.com/a/28385677/1428052

关于algorithm - 我们如何从 LCP 阵列构建 LCP-LR 阵列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38128092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com