首页 > 其他分享 >[模板]kmp求Next数组

[模板]kmp求Next数组

时间:2022-11-17 17:05:12浏览次数:40  
标签:getNext patternLen int pattern next kmp Next 模板

模板

#include <iostream>
#include <string>

using namespace std;

void getNext(const string& p,int next[])
{

    int len = (int)p.size();
    next[0] = -1;
    int k = -1;
    int j = 0;
    while(j < len - 1){
        if(k == -1 || p[j] == p[k]){
            j++;
            k++;
            next[j] = k;
        }else{
            k = next[k];
        }
    }
}

int main()
{
    string pattern;
    cin>>pattern;
    int patternLen = (int)pattern.size();
    int next[patternLen] = {0};
    getNext(pattern,next);
    for(int i=0;i<patternLen;i++){
        printf("%d ",next[i]);
    }
    return 0;
}

 

标签:getNext,patternLen,int,pattern,next,kmp,Next,模板
From: https://www.cnblogs.com/zbsy-wwx/p/16900013.html

相关文章

  • js中的模板字符串问题
    在写js的字符串时,虽然单双引号都用了,但是${}修饰的字符串却始终没有正确替换为变量,最后查了一下语法,发现和python中不同,js中的模板字符串是需要用反引号的,而不是一般引号,就......
  • LaTeX简单常用方法笔记,附模板
    简单用用吧,为了完成作业,3~5分太香了具体用法可自行搜索推荐LaTeX在线编辑器:https://cn.overleaf.com/标题:​​\title{标题}​​作者:​​\author{作者}​​学号:​​\studenti......
  • 5 Rust game engines to consider for your next project
    https://blog.logrocket.com/5-rust-game-engines-consider-next-project/ May20,2022  5minread MoreandmoredevelopersarechoosingRustoverC++......
  • ES6之模板字符串
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>模板......
  • 【模板】多项式乘法 FFT
    postedon2022-08-0223:57:12|under模板|source膜拜,抄写problem\[c_k=\sum_{i+j=k}a_ib_j.\]\(a,b\)已知,要求\(O(n\logn)\)。prework:复数定义初中数学中......
  • 1010002504-软件工程基础Y-吕书海 实验二 结对项目报告模板 (1).docx
    《软件工程基础》上机实验报告撰写要求 一、 纸张与页面要求采用国际标准A4型打印纸或复印纸,纵向打印。封页和页面按照下面模板书写(正文为:小四宋体1.5倍行距,首行......
  • 1010002504-软件工程基础Y-实验一 吕书海个人项目报告模板
    《软件工程基础》上机实验报告撰写要求 一、 纸张与页面要求采用国际标准A4型打印纸或复印纸,纵向打印。封页和页面按照下面模板书写(正文为:小四宋体1.5倍行距)。图......
  • 【模板】数学
    同余逆元线性递推inv[1]=1;for(inti=2;i<=n;++i) inv[i]=p-inv[p%i]*(p/i)%p,inv[i]%=p;快速幂inv[i]=ksm(i,p-2,p)阶乘递推inv[n]=ksm(po[n],p-2,p);//po[i]是......
  • 【模板】快速沃尔什变换 FWT
    解决形如\[c_k=\sum_{i\oplusj=k}a_ib_j.\]的卷积式子,这里解决\(\oplus=\{\cup,\cap,\text{xor}\}\)。#include<cstdio>#include<cstring>#include<algorithm>u......
  • 模板奇异递归+扩展方法
    #include<iostream>template<classderived>structbase{derivedgetDerivedType(){};voidinterface(){static_cast<derived*>(this)->interface();};};s......