最小表示法 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;
        }
    }
}

 

基于STM32F4XX 、HAL库的SHT40-CD1B-R3温湿度传感器驱动应用C语言程序设计 define SHT40_ADDR_READ 0x8B // CD1B 读地址 ((0x45 << 1) + 1)#define SHT40_ADDR_WRITE 0x8A // CD1B 写地址 (0x45 << 1)// 初始化 I2C1 (根据 CubeMX 生成的函数)// 湿度: -6%RH + 125%RH * (raw / 2^16)// 温度: -45°C + 175°C * (raw / 2^16)// 返回值: 0=成功, 1=失败(CRC错误或I2C通信错误) 阅读详情

相关推荐

数字IC设计避坑指南:为什么477条net的DRV问题无法修复?解析don‘t touch hierarchy的影响

本文深入解析了数字IC后端实现中,因“don't touch hierarchy”约束导致大量DRV违规(如477条net)无法自动修复的根本原因。文章以Cadence Innovus工具为例,详细阐述了DRV修复机制与设计保护约束的冲突原理,并提供了从日志定位、风险评估到分级解除约束的完整实战策略,帮助工程师有效解决此类设计收敛难题。

hope8的博客 290

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

【SOC 芯片设计 DFT 学习专栏 -- DFT DRC规则检查】

在现代半导体设计中,DFT(Design for Test)技术的重要性不言而喻。它确保了芯片在制造完成后能够进行高效和可靠的测试。DFT设计规则检查(DRC)作为DFT流程中的一个关键环节,通过一系列自动化工具检测设计中的潜在问题,从而保证设计的可测试性和整体质量。在本文中,我们将详细探讨DFT DRC的概念、重要性、检查流程以及具体的规则和应用。Tessent是业界领先的DFT解决方案之一,其DRC工具通过一系列自动化检查,确保设计符合DFT要求。

CodingCos的博客 4170

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

从QEvent到系统响应:EC如何成为硬件事件的隐形信使

本文深入解析嵌入式控制器(EC)如何作为硬件事件的隐形信使,通过QEvent机制将按键、合盖等物理事件转换为SCI信号,并经由ACPI架构触发操作系统响应。EC与BIOS紧密协作,实现了从底层硬件到系统层面的高效事件传递,是嵌入式开发和系统优化的关键技术。

yellow的专栏 651

干法刻蚀工艺进阶:如何通过调节DC自偏压优化刻蚀速率和各向异性

本文深入探讨了干法刻蚀工艺中DC自偏压的精细调控艺术。DC自偏压是影响刻蚀速率和各向异性的关键因素,文章通过实战案例,详细分析了如何通过调节射频功率、频率、腔室压力及气体成分等参数,间接而有效地控制DC自偏压,从而优化高深宽比硅沟槽等复杂结构的刻蚀效果,实现工艺性能的精准提升。

weixin_28308325的博客 334

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

[南大ICS-PA2] 字符串处理函数和printf实现

[南大ICS-PA2] 程序、运行时环境与AM 字符串处理函数和printf实现

qq_42474104的博客 2987

[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

STM32笔记:高精度脉冲宽度计 双输入捕获+DMA方式

本文介绍如何用STM32F107VC实现高精度的脉冲宽度计(测量周期脉冲的占空比)。 采用定时器的外部输入捕获功能,双输入+DMA方式,追求硬件的极限表现。

st01lsp的专栏 5289

经纬度转换为UTM坐标

UTM(通用横向墨卡托投影): 是一种以米为单位的坐标系统,用于地图和GPS导航。将地球划分为60个纵向的区域。每个区域宽6度,从赤道开始往南北两级方向划分。每个区域都有一个特定的字母。

weixin_45626706的博客 7278

字符串__

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

ZZZS0516的博客 1278

计算机视觉与深度学习 | 基于Matlab的多特征融合可视化指纹识别系统(附完整代码)

此系统展示了一个完整的指纹识别流程,实际应用中可以根据需求进一步优化算法,如改进方向场计算、增加脊线频率特征、使用更精确的匹配算法等。

尘世冰封的专栏 362

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

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

污miao汪的博客 513

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

算法书签

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

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

数据结构与算法: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、付费专栏及课程。

余额充值