最小表示法 KMP 扩展KMP Manacher 模板

kmp习题大全 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70325#overview kuangbin带你飞专题十六kmp&扩展kmp&manacher A. 题意:给T组数据,每组有长度为n和m的母串和模式串。判断模式串是否是母串的子串,如果是输出最先匹配完成的位置,否则输出-1. 做法:直接套用模板。把char改成int。kmp函数中 阅读详情

最小表示法+KMP

String Problem

 HDU - 3374 

Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings: 
String Rank 
SKYLONG 1 
KYLONGS 2 
YLONGSK 3 
LONGSKY 4 
ONGSKYL 5 
NGSKYLO 6 
GSKYLON 7 
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once. 
  Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also. 

Input

  Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.

Output

Output four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.

Sample Input

abcder
aaaaaa
ababab

Sample Output

1 1 6 1
1 6 1 6
1 3 2 3
#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
#include<set>
#include<queue>
using namespace std;
char ss[1000005];
int net[1000005];
int getminindex()
{
    int len=strlen(ss);
    int i=0,j=1,k=0;
    while(i<len&&j<len&&k<len){
        if(ss[(i+k)%len]==ss[(j+k)%len])
            k++;
        else if(ss[(i+k)%len]<ss[(j+k)%len]){
            j=j+k+1;
            k=0;
        }
        else{
            i=i+k+1;
            k=0;
        }
        if(i==j)
            j++;
    }
    return i<j?i:j;
}
int getmaxindex()
{
    int len=strlen(ss);
    int i=0,j=1,k=0;
    while(i<len&&j<len&&k<len){
        if(ss[(i+k)%len]==ss[(j+k)%len])
            k++;
        else if(ss[(i+k)%len]>ss[(j+k)%len]){
            j=j+k+1;
            k=0;
        }
        else{
            i=i+k+1;
            k=0;
        }
        if(i==j)
            j++;
    }
    return i<j?i:j;
}
void getnet()
{
    int len=strlen(ss);
    for(int i=1;i<len;i++){
        int j=net[i-1];
        while(ss[j]!=ss[i]&&j>0)
            j=net[j-1];
        if(ss[j]==ss[i])
            net[i]=j+1;
        else
            net[i]=0;
    }
}
int main()
{
    while(scanf("%s",ss)!=-1){
        getnet();
        int len=strlen(ss);
        int mincycle=len-net[len-1];
        int m=1;
        if(len%mincycle==0)
            m=len/mincycle;
        printf("%d %d %d %d\n",getminindex()+1,m,getmaxindex()+1,m);
    }
    return 0;
}

Manacher

T - Palindrome

 POJ - 3974 

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?" 

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not. 

The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!". 

If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

Input

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity). 

Output

For each test case in the input print the test case number and the length of the largest palindrome. 

Sample Input

abcbabcbabcba
abacacbaaaab
END

Sample Output

Case 1: 13
Case 2: 6
#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
#include<set>
#include<queue>
#include<map>
#include<iostream>
#include<string>
using namespace std;
const int num=1000010;
int r[num*2];
char ss[num],ssa[num*2];
int get_r()
{
    ssa[0]='#';
    int len=1;
    int lenss=strlen(ss);
    for(int i=0; i<lenss; i++)
    {
        ssa[len++]=ss[i];
        ssa[len++]='#';
    }
    ssa[len]='\0';
    int bound=0,cent=0,maxr=0;
    for(int i=0; i<len; i++)
    {
        if(i<bound)
            r[i]=min(r[cent*2-i],bound-i);
        else
            r[i]=0;
        while(i-r[i]-1>=0&&i+r[i]+1<len&&ssa[i-r[i]-1]==ssa[i+r[i]+1])
            r[i]++;
        if(bound<i+r[i])
        {
            cent=i;
            bound=i+r[i];
        }
        maxr=max(maxr,r[i]);
    }
    return maxr;
}
int main()
{
    int cnt=1;
    while(scanf("%s",ss)!=-1)
    {
        if(ss[0]=='E')
            break;
        memset(r,0,sizeof(r));
        printf("Case %d: %d\n",cnt++,get_r());
    }
    return 0;
}

扩展KMP

const int maxn=100010;
int net[maxn],ex[maxn];
void getnet(char* s2)
{

    int i=0,j,po,len=strlen(s2);
    net[0]=len;
    while(s2[i]==s2[i+1]&&i+1<len)
        i++;
    net[1]=i;
    po=1;
    for(i=2; i<len; i++){
        if(net[i-po]+i<net[po]+po)
            net[i]=net[i-po];
        else{
            j=net[po]+po-i;
            if(j<0)
                j=0;
            while(i+j<len&&s2[j]==s2[j+i])
                j++;
            net[i]=j;
            po=i;
        }
    }
}
void getex(char* s1,char* s2)
{
    int i=0,j,po,len1=strlen(s1),len2=strlen(s2);
    getnet(s2);
    while(s1[i]==s2[i]&&i<len1&&i<len2)
        i++;
    ex[0]=i;
    po=0;
    for(i=1; i<len1; i++)
    {
        if(net[i-po]+i<ex[po]+po)
            ex[i]=net[i-po];
        else{
            j=ex[po]+po-i;
            if(j<0)
                j=0;
            while(i+j<len1&&j<len2&&s1[j+i]==s2[j])
                j++;
            ex[i]=j;
            po=i;
        }
    }
}

 

不同比例尺地质数据共享 包含20w、25w、100w、250w比例尺地质数据共享。 阅读详情

相关推荐

Java后端实战:钉钉互动卡片消息从配置到生产级对接

在企业级消息推送场景中,机器人通知已从简单的文本向富交互演进。其核心原理在于通过可交互的卡片模板,将传统单向通知升级为双向、有状态的微型应用界面,实现了用户无需跳转即可完成操作的技术价值。这广泛应用于审批、任务确认、数据填报等需要即时反馈的业务流程。本文聚焦于钉钉平台,深入剖析其互动卡片消息机制,并基于Java后端,系统讲解如何实现包括安全验签、异步回调与卡片动态更新在内的生产级对接方案,涵盖配置管理、服务降级等工程实践关键点,助力构建稳定高效的企业消息交互服务。

weixin_30617737的博客 439

kmp-KMP入门必做-匹配

Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a

Tyshawn的博客 728

ISP Tuning实战:从零开始调校相机传感器的完整流程(附避坑指南)

本文详细介绍了ISP Tuning的完整实战流程,即如何从零开始调校相机传感器以优化成像质量。内容涵盖从黑电平校正、镜头阴影校正等基础标定,到自动曝光、伽马校正、色彩校正等核心画质调校,再到降噪与锐化的平衡策略,并提供了关键的避坑指南。掌握ISP Tuning是提升相机传感器性能、实现从‘能用’到‘好用’的关键技术。

weixin_32098457的博客 196

HDU 3374(最小最大表示法+KMP)

传送门 String Problem Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4445Accepted Submission(s): 1814 Problem Description Gi...

weixin_30487201的博客 155

Palantir的Ontology:从静态知识图谱到动态业务引擎的跃迁

本文探讨了Palantir的Ontology技术如何将静态知识图谱升级为动态业务引擎。通过引入函数和行动作为核心元素,Ontology实现了业务逻辑的自动化响应与执行,显著提升物流、能源等领域的运营效率。文章详细解析了其混合存储引擎、状态监听网络等关键技术设计,并对比展示了传统架构与Ontology架构的业务价值差异。

weixin_42570296的博客 200

Western blot (WB) 灰度分析进阶指南:ImageJ 高效定量技巧与实战优化

本文详细介绍了使用ImageJ进行Western blot灰度分析的进阶技巧,包括批量处理、背景校正和疑难条带处理等实战优化方法。通过自动化宏脚本和动态背景校正,显著提升分析效率和准确性,特别适合科研人员处理复杂WB数据。

weixin_42557830的博客 901

Manacher模板,kmp,扩展kmp,最小表示法模板

// Manacher算法,很好用。char s[2*N]; //储存临时串 int save[2*N];//中间记录 int Manacher(char tmp[]) { int len=strlen(tmp); int cnt=0; for(int i=0;i<len;i++) { s[cnt++]='#'; ...

weixin_30384031的博客 75

开发板的使用

一、开发板使用学习大纲。 1、开发板组成、核心板资源,底板资源。 2、开发板连接、调试工具、串口参数配置。 3、开发板启动过程。 4、文件传输 1)使用串口传输。 上传/下载 2)使用网口传输。 上传/下载 5、临时/永久配置开发板IP地址。 6、开发板启动脚本。 二、开发板的组成。 核心板: CPU处理器: 芯片:S5P6818(三星) 内核: ARM cortex-A53 -> 八核处理器 运行内存:512MB*2=1G nandflash:4G 底板: 电源线:5V 串口...

qq_45633736的博客 2659

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher

可算在学校大佬的带领下 正经学了点算法了 人的专注力不行啊我 挂个题目链接 24道题搞定了21,看其他的完成率,估计不行了 当然还是有些看了题解的。 写这个主要是为了总结一下板子,和思路吧,以后好直接抄,嘿嘿 KMP模板 next数组 next_[0] = -1; int i = 0, j = -1; while (i < e) {//e字符串c1长度 if (j == -1 || c1[i] == c1[j]) { i++; j++; next_[i] = j

qq_45810570的博客 420

【Python】秋招机考 —— 2025/8/27 华为 AI 方向

2025 华为 AI 秋招机考题目包含15道单选题、5道多选题和2道机器学习编程题,涵盖核心算法与数学知识。单选题考察了Softmax激活、Transformer位置编码、Jacobi迭代、大模型幻觉现象、特征向量、K-means聚类、矩阵秩、实对称矩阵性质、SVD分解、概率论等知识点。多选题涉及高斯-赛德尔迭代和朴素贝叶斯分类器。

一杯水果茶!足矣~ 841

[系统安全] 十六.PE文件逆向基础知识(PE解析和PE编辑工具)

您可能之前看到过我写的类似文章,为什么还要重复撰写呢?只是想更好地帮助初学者了解病毒逆向分析和系统安全,更加成体系且不破坏之前的系列。因此,我重新开设了这个专栏,准备系统整理和深入学习系统安全、逆向分析和恶意代码检测,“系统安全”系列文章会更加聚焦,更加系统,更加深入,也是作者的慢慢成长史。换专业确实挺难的,逆向分析也是块硬骨头,但我也试试,看看自己未来四年究竟能将它学到什么程度,漫漫长征路,偏向虎山行。享受过程,一起加油~ 作者前面介绍了三个漏洞,包括Chrome浏览器保留密码渗透解析、通过Chrome浏

杨秀璋的专栏 1万+

字符串__

鹅,鹅,鹅,曲项向天歌。白毛浮绿水,红掌拨清波。

ZZZS0516的博客 1278

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

kmp&扩展kmp&manacher&最大最小表示法模板

kmp&扩展kmp&manacher&最大最小表示法模板

污miao汪的博客 513

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

算法书签

2019-8-5 逆序对:树状数组求逆序对 2019-7-18 卡特兰数:小思Catalan数 2019-6-30 0/1分数规划:0/1 分数规划详解 2019-5-6 最小表示法:最小(大)表示法模板及详解 2019-5-4 康托展开:康托展开和逆康托展开 2019-4-24 Manacher算法:Manacher 2019-4-21 AC自动机:AC自动机算法详解 ...

悲伤无声地涌出来 943

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

数据结构与算法:Manacher算法和扩展KMP

Manacher算法和扩展KMP

2402_89326161的博客 738

KMP、扩展KMP、MANACHER

一、KMP 作用:用于在一个文本串S内查找一个模式串P出现的位置 string str = "bacbababadababacambabacaddababacasdsd"; string ptr = "ababaca"; 如上图,可得在第10与26处包含ptr数组; 暴力做法:暴力for,碰到不一样的直接返回,从后一个开始继续for,最差能到O(n * m) K...

dmp63404的博客 210

kuangbin 专题十六:KMP & 扩展KMP Manacher

1. Number Sequence 题解:这是一道kmp的模板题目套版即可 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 1000010; int n, m, t, f; int ne[N]; int a[N], b[N]; //获取next数组 void getNe() {

weixin_45867800的博客 537

2025年河南省暑期青少年程序设计能力提升集训DAY2线性字符串算法(哈希+KMP+Manacher+最小表示法)

关于哈希的介绍这里复制粘贴了:Hash是一种数据结构,叫做Hash表(哈希表),也叫散列表。关于Hash的实现,其实与离散化颇为类似。就是把若⼲的复杂的信息映射到一个比较容易维护的值域去。具体的实现方式是散列函数,即Hash函数,其原理是对于一个数据,选取一个关键键值 ,那么这个数据在Hash表中的位置就是。那么这个对应关系就是哈希函数。即:(p为大质数,常用13331,mod取更大即可,我用无符号long long不考虑mod)个人理解就是一段。

Wander_ner的博客 908

kuangbin专题十六 KMP & 扩展KMP & Manacher

kuangbin专题十六 KMP & 扩展KMP & Manacher 1.HDU - 1711 kmp模板题,只是从字符串变成了整数数组。 #include<bits/stdc++.h> using namespace std; const int N=1e6+10; int a[N],b[N]; int n,m,ne[N]; int main() { int T; scanf("%d",&T); while(T--){ scan

qq_43657674的博客 421

Manacher算法、扩展Kmp

从第一个字符开始枚举间隔k长度的字符是否和原字符串的前缀完全相同:如果相同,那么跳过几个间隔就花费几秒;如果到字符串最后还没匹配上,那么字符串的总长度除以向上取整就是花费的时间。解决:在每两个字符之间及字符串的左右两头都插上任意字符(扩展串),然后枚举每个点向左右两边扩展,得到的扩展半径-1就是回文串长度。z数组:一个字符串从每个位置开始的字串和原字符串开头匹配上的最长公共前缀长度。e数组:字符串a的每一个位置和字符串b的开头匹配上的最长公共前缀长度。求一段字符串中的最长回文字符串。

2301_80275822的博客 309

Kuangbin专题十六 - KMP & 扩展KMP & Manacher模板总结

从2019/12-28 - 2020/1/26 学习了kuangbin专题中题量最大的“KMP & 扩展KMP & Manacher”专题。 在此记录下自己痛苦的蜕变和学习获得的关于各类字符串处理模板和心得。 学习心得 经过这次半个月的自主学习,这确实是和在学校完全不同的感受。 在学校的话经常是天天忙于其他事情,反而想敲代码的欲望非常的强烈,然而回家后因为没有...

weixin_45915254的博客 248

[模板] 扩展KMP

一、题目 点此看题 二、解法 这个算法虽然和 kmp\tt kmpkmp 或者是马拉车。 我们先从题目要求的第一个数组入手,设 nxt[i]nxt[i]nxt[i] 为 bbb 和 bbb 的后缀 iii 的最长公共前缀。我们设 i+nxt[i]i+nxt[i]i+nxt[i] 覆盖最远的点是 ppp(这是我们以前就维护出来的),转移分两种情况讨论: 情况 111 : ...

C202044zxy的博客 452

洛谷 P5410 【模板】扩展KMP

题目链接 https://www.luogu.org/problem/P5410 分析 国外一般称此算法为Z Algorithm,个人感觉算法思想上和Manacher算法更为接近; 设 next[i]next[i]next[i] 表示模式串 SSS 后缀 i...ni ... ni...n 与 SSS 的最大公共前缀长度; 若已求 next[1...x−1]next[1 ... x - 1]nex...

Kevin的博客 264
上一篇: HDU - 2852 树状数组查询第k大的数
下一篇: 树链剖分模板 洛谷 P3384
Chter0
博客等级 码龄8年 13粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值