Manacher模板,kmp,扩展kmp,最小表示法模板
- // Manacher算法,很好用。
- char s[*N]; //储存临时串
- int save[*N];//中间记录
- int Manacher(char tmp[])
- {
- int len=strlen(tmp);
- int cnt=;
- for(int i=;i<len;i++)
- {
- s[cnt++]='#';
- s[cnt++]=tmp[i];
- }
- s[cnt++]='#';
- memset(save,,sizeof(save));
- int a=,p=;
- int mx=;
- for(int i=;i<cnt-;i++)
- {
- if(i>=p)
- {
- int num=;
- while(i+num<cnt&&i-num>=&&s[i+num]==s[i-num])
- {
- num++;
- }
- p=i+num-; //能到达的最远位置
- a=i;
- save[i]=num-; //从这个位置出发最长的回文
- if(save[i]==) continue;
- if(s[i]=='#')
- {
- if(*((save[i]-)/ +)>mx) mx=*((save[i]-)/+);
- }
- else
- {
- if(*(save[i]/)+>mx) mx=*(save[i]/)+;
- }
- }
- else
- {
- int j=*a-i;
- if( i+save[j] < p)
- {
- save[i]=save[j];
- if(save[i]==) continue;
- if(s[i]=='#')
- {
- if(*((save[i]-)/ +)>mx) mx=*((save[i]-)/+);
- }
- else
- {
- if(*(save[i]/)+>mx) mx=*(save[i]/)+;
- }
- }
- else
- {
- int k=*i-p;
- while(p+<cnt&&k->=&&s[p+]==s[k-])
- {
- p++;
- k--;
- }
- a=i;
- save[i]=p-i;
- if(save[i]==) continue;
- if(s[i]=='#')
- {
- if(*((save[i]-)/ +)>mx) mx=*((save[i]-)/+);
- }
- else
- {
- if(*(save[i]/)+>mx) mx=*(save[i]/)+;
- }
- }
- }
- }
- return mx;
- }
kmp,扩展kmp
- // 感觉kmp可以理解。
- #define N 100010
- int s[N];
- int next[N];
- int t[N];
- int main()
- {
- //freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
- //freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
- int T;
- scanf("%d",&T);
- while(T--)
- {
- int n,m;
- scanf("%d%d",&n,&m);
- for(int i=;i<n;i++)
- scanf("%d",s+i);
- for(int i=;i<m;i++)
- scanf("%d",t+i);
- ///////////////求next
- int j=;
- next[]=;
- for(int i=;i<m;i++)
- {
- while(j!=&&t[j]!=t[i]) j=next[j-];
- if(t[j]==t[i]) j++;
- next[i]=j;
- }
- /////////////////////
- j=;
- int ans=-;
- for(int i=;i<n;i++)
- {
- while(j!=&&s[i]!=t[j]) j=next[j-];
- if(s[i]==t[j]) j++;
- if(j==m)
- {
- ans=i+-m+;
- break;
- }
- }
- ///////////////////
- if(ans==-)
- printf("-1\n");
- else
- printf("%d\n",ans);
- }
- return ;
- }
- // 扩展kmp。
- #defien N
- int next[N],sum[N];// sum用来记录
- void build(char s[])
- {
- memset(next,,sizeof(next));
- int p=,a=;
- next[]=len;
- for(int i=;i<len;i++)
- {
- if(i+next[i-a]-<p)
- {
- next[i]=next[i-a];
- }
- else
- {
- int k=p-i;
- while(p+<len && s[p+]==s[k+])
- {
- p++; k++;
- }
- p=max(p,i);
- next[i]=k+;
- a=i;
- }
- }
- }
- void extend_kmp(char s[],char t[])//求从s到t上的最大前缀
- {
- memset(sum,,sizeof(sum));
- int p=-,a=;
- for(int i=;i<len;i++)
- {
- if(i+next[i-a]-<p)
- {
- sum[i]=next[i-a];
- }
- else
- {
- int k=p-i;
- while(p+<len && s[p+]==t[k+])
- {
- p++; k++;
- }
- p=max(p,i);
- sum[i]=k+;
- a=i;
- }
- }
- }
最小表示法
- int mistr(char s[],int len) //输入一串字符,返回最小表示法的起始位置
- {
- int i=,j=,k=;
- while(i<len&&j<len&&k<len)
- {
- if(s[i+k]==s[j+k])
- {
- k++;
- }
- else
- {
- if(s[i+k] > s[j+k]) i=i+k+;
- if(s[i+k] < s[j+k]) j=j+k+;
- k=;
- if(i==j) j++;
- }
- }
- return min(i,j);
- }
Manacher模板,kmp,扩展kmp,最小表示法模板的更多相关文章
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher 题解报告
来刷kuangbin字符串了,字符串处理在ACM中是很重要的,一般比赛都会都1——2道有关字符串处理的题目,而且不会很难的那种,大多数时候都是用到一些KMP的性质或者找规律. 点击标题可跳转至VJ比赛 ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 浅谈Manacher算法与扩展KMP之间的联系
首先,在谈到Manacher算法之前,我们先来看一个小问题:给定一个字符串S,求该字符串的最长回文子串的长度.对于该问题的求解.网上解法颇多.时间复杂度也不尽同样,这里列述几种常见的解法. 解法一 ...
- P5410 【模板】扩展 KMP
P5410 [模板]扩展 KMP #include<bits/stdc++.h> using namespace std; ; int q, nxt[maxn], extend[maxn] ...
- 题解-洛谷P5410 【模板】扩展 KMP(Z 函数)
题面 洛谷P5410 [模板]扩展 KMP(Z 函数) 给定两个字符串 \(a,b\),要求出两个数组:\(b\) 的 \(z\) 函数数组 \(z\).\(b\) 与 \(a\) 的每一个后缀的 L ...
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法
[KMP] 学习KMP,我们先要知道KMP是干什么的. KMP?KMPLAYER?看**? 正如AC自动机,KMP为什么要叫KMP是因为它是由三个人共同研究得到的- .- 啊跑题了. KMP就是给出一 ...
- ACM之路(12)—— KMP & 扩展KMP & Manacher
最近做完了kuangbin的一套关于kmp的题目(除了一道字典树的不会,因为还没学字典树所以先放放),做个总结.(kuangbin题目的链接:http://acm.hust.edu.cn/vjudge ...
- Kuangbin 带你飞 KMP扩展KMP Manacher
首先是几份模版 KMP void kmp_pre(char x[],int m,int fail[]) { int i,j; j = fail[] = -; i = ; while (i < m ...
随机推荐
- pandas知识点汇总
## pandas基础知识汇总 1.时间序列 import pandas as pd import numpy as np import matplotlib.pyplot as plt from d ...
- Eclipse - JAR包制作
Eclipse - JAR包制作细节 1.Jar包分为两种,一种是不可运行的,一种是可运行的Jar包,他们的主要区别如下: > 不可直接运行的Jar包主要是用于给别的程序提供调用 ...
- TCP网络传输, 数据类型的问题
转载: http://blog.csdn.net/highfly591/article/details/45309239 1.采用TCP传输时, 应用层为什么要做超时重传: tcp保证数据可靠传输,传 ...
- mysql导入慢
MySQL导出的SQL语句在导入时有可能会非常非常慢,经历过导入仅45万条记录,竟用了近3个小时.在导出时合理使用几个参数,可以大大加快导 入的速度. -e 使用包括几个VALUES列表的多行INSE ...
- loading数据加载的6种形式
数据加载的几种形式及对应的交互设计 1.全屏加载 多出现在H5页面,例如微信的文章详情页.全屏加载的特点是数据一次性加载完成,内容加载完成之前界面都会停留在loading界面.进度条和有趣的动画设计, ...
- Windows下Python添加MySQLdb扩展模块
[更新 2012-09-16] 这里可以下载已经打包好的EXE文件,http://sourceforge.net/projects/mysql-python/(国内需穿越才可访问) DBank备份下载 ...
- Atitit.antlr实现词法分析
Atitit.antlr实现词法分析 1.1. antlrworks-1.4.3.jar wizard1 1.2. 词法的类型 id,int,float ,comment,str,char,wh ...
- [sh]清理memcached缓存
#!/bin/bash ###author xxx ###date xxx ###清理内存缓存 used=`free -m | awk 'NR==2' | awk '{print $3}'` free ...
- QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout
http://blog.csdn.net/zhuyingqingfen/article/details/6562246 如题,出现这个的原因是,如果你的窗口继承的是QMainwindow,需要设置 s ...
- Linux之精灵进程
一.引言 工作中有时候可能会写一些这样的程序,它作为后台进程运行,生命周期比一般的进程要长,它在系统开机时运行,直到被强制关闭或者系统关机时退出.它就是精灵进程或者也叫做守护进程--daemon pr ...