首页 > 其他分享 >Z algorithm

Z algorithm

时间:2024-09-06 12:46:37浏览次数:4  
标签:box algorithm int vector KMP include

Z函数也叫扩展KMP算法,因为思路和KMP很像,都是从已经获得的信息中处理后面的信息。

思路参考:

灵茶山艾府-Z函数 (听了这个才听懂)

OI WIKI

算法demo程序

模板:
#include<vector>
#include<cstring>
#include<iostrea>
using namespace std;

vector<int> (string s)
{
    int len = s.size();

    vector<int> z(n);

    for (int i = 1, l = 0, r = 0; i < n;i ++)
    {
        if(i <= r && z[i - 1] < r - i + 1)//首先处理可以直接等的情况
        {
            z[i] = z[i - l];
        }else
        {
            z[i] = max(0, r - i + 1); //r - i + 1 等于i - 1时,在z-box里面的把从i到r占满了,所以要往后暴力枚举
            while (i + z[i] < n && s[z[i]] == s[i + z[i]] )
                z[i]++;
            
        }
        if (i + z[i] - 1 > r)
            l = i, r = i + z[i] - 1; //当匹配的新串的长度比z-box的范围要大就更新z-box
    }
    return z;//返回z
}

标签:box,algorithm,int,vector,KMP,include
From: https://www.cnblogs.com/chhh31/p/18400012

相关文章

  • C++(#include <algorithm>)
    目录1.std::sort2.std::reverse3.std::find4.std::copy5.std::equal6.std::for_each7.std::unique8.std::transform总结#include<algorithm>是C++标准库中的一个头文件,包含了许多常用的算法函数,提供了操作容器、范围和数据的功能。这个库中的算法大多数是通用的,可......
  • Study Plan For Algorithms - Part22
    1.字符串相乘题目链接:https://leetcode.cn/problems/multiply-strings/给定两个以字符串形式表示的非负整数num1和num2,返回num1和num2的乘积,它们的乘积也表示为字符串形式。classSolution:defmultiply(self,num1:str,num2:str)->str:ifnum1==......
  • Study Plan For Algorithms - Part21
    1.缺失的第一个正数题目链接:https://leetcode.cn/problems/first-missing-positive/给定一个未排序的整数数组nums,请找出其中没有出现的最小的正整数。classSolution:deffirstMissingPositive(self,nums:List[int])->int:n=len(nums)forii......
  • Study Plan For Algorithms - Part20
    1.组合总和题目链接:https://leetcode.cn/problems/combination-sum/给定一个无重复元素的整数数组candidates和一个目标整数target,找出candidates中可以使数字和为目标数target的所有不同组合,并以列表形式返回。classSolution:defcombinationSum(self,ca......
  • Study Plan For Algorithms - Part20
    1.树的子结构输入两棵二叉树A和B,判断B是不是A的子结构。方法一:classTreeNode:def__init__(self,val=0,left=None,right=None):self.val=valself.left=leftself.right=rightdefisSubStructure(A,B):ifnotAornot......
  • Study Plan For Algorithms - Part18
    1.搜索插入位置题目链接:https://leetcode.cn/problems/search-insert-position/给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。classSolution:defsearchInsert(self,nums:List[int],target:......
  • COMP20003 Algorithms and Data Structures Spellcheck Lookup
    Assignment2:SpellcheckLookupGeneralYoumustreadfullyandcarefullytheassignmentspecificationandinstructions.Course:COMP20003AlgorithmsandDataStructures@Semester2,2024DeadlineSubmission:Friday6thSeptember2024@11:59pm(endo......
  • C++头文件<algorithm>中常用函数简介
     概述头文件algorithm(算法库)中主要提供了一些对容器操作的函数,如排序、搜索、复制、比较等,因此使用频率还是很高的,由于主要是操作容器,所以函数的语法也很类似:algorithm_name(container.begin(),container.end(),...);其中,container.begin()和container.end()分......
  • Study Plan For Algorithms - Part16
    1.下一个排列题目链接:https://leetcode.cn/problems/next-permutation/整数数组的一个排列就是将其所有成员以序列或线性顺序排列。整数数组的下一个排列是指其整数的下一个字典序更大的排列。更正式地,如果数组的所有排列根据其字典顺序从小到大排列在一个容器中,那么数组......
  • Dijkstra's algorithm All In One
    Dijkstra'salgorithmAllInOne迪杰斯特拉算法DijkstraDijkstra'salgorithm(/ˈdaɪkstrəz/DYKE-strəz)isanalgorithmforfindingtheshortestpathsbetweennodesinaweightedgraph,whichmayrepresent,forexample,roadnetworks.Dijkstra算法是一种......