我如何使用z算法来完成这个任务
How can I do this task using Z-algorithm?
在一个问题中,我被要求查找给定字符串s是否包含两个不重叠的子字符串"AB"answers"BA"(子字符串可以以任何顺序进行)。我已经解决了这个问题,但由于我正在学习 z -算法。有人能帮我吗?
我知道如何在文本中找到模式的出现次数(通过附加p和T),但我不知道如何使用Z算法解决这个问题?
用z -算法查找T是否包含p:
S = P + '@' + T //extra char not occurring in strings
for i in 0..Length(T) - 1 do
if Z[i + Length(P) + 1] = Length(P) then
P contains T in ith position
查找T是否同时包含'AB'和'BA'且不重叠:
Sab = 'AB@' + T
Sba = 'BA@' + T
Build Zab and Zba arrays with Z-algo
PosAB_Last = Length(T) + 10 //just big value
PosAB_Prev = PosAB_Last
PosBA_Last = PosAB_Last
PosBA_Prev = PosAB_Last
for i in 0..Length(T) - 1 do
if Zab[i + 3] = 2 then
PosAB_Prev = PosAB_Last //keep two last positions of AB in text
PosAB_Last = i
//it is enough to compare positions with two last occurences of 'BA '
//so algo is linear
if (i - PosBA_Last > 1) or (i - PosBA_Prev > 1) then
Success
else
if Zba[i + 3] = 2 then
PosBA_Prev = PosBA_Last
PosBA_Last = i
if (i - PosAB_Last > 1) or (i - PosAB_Prev > 1) then
Success
相关文章:
- 命令模式 - 使用"weight"执行任务的命令
- 对于下面给出的任务,使用类或多维数组更好吗
- 如何使用任务栏按钮显示进度条?
- 无法在 VS Code 中多次使用预启动任务
- 使用 UWP 中的 IAsyncOperation 继续执行任务
- 如何使用QSplashScreen时显示任务栏图标
- 使用 class 在 C++ 中表示任务
- 使用两个任务之间的静音信号
- 加密任务使用QT崩溃,Openssl该怎么办
- 在此任务上遇到麻烦,我们需要使用指针和数组来找到文本文件中某些数字的平均值,中值和模式
- 使用 com 对象删除计划任务
- 单元测试任务使用 QThreadPool 并行运行
- 如何使用 c++ 在任务计划程序中检查任务的触发器
- 使用BFS/DFS解决编程任务
- 使用委托从后台任务更新 UI
- 在将函数分配给函数指针时,为什么可以多次使用取消任务运算符
- 使用win32监视Windows任务栏状态更改
- 使用C 多任务.线程或不同的设计
- 日历和做任务列表使用数据结构
- 我应该在openMP并行区域(for循环,任务)内使用gnu并行模式函数吗?