首页 > 其他分享 >【模板】Z 函数(扩展 KMP)

【模板】Z 函数(扩展 KMP)

时间:2022-11-06 19:25:01浏览次数:73  
标签:函数 int ir char 20000010 KMP include 模板

posted on 2022-08-08 23:29:53 | under 模板 | source

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int n,m,z[20000010],p[20000010];
char a[20000010],b[20000010];
void getz(char *a,int n){
	z[1]=n;
	for(int i=2,l=0,r=0;i<=n;i++){
		if(i<=r) z[i]=min(z[i-l+1],r-i+1);
		while(i+z[i]<=n&&a[i+z[i]]==a[z[i]+1]) z[i]++;
		if(i+z[i]-1>r) r=i+z[l=i]-1; 
	}
}
void match(char *a,int n,char *b,int m){
	b[m+1]='?';
	for(int i=1,l=0,r=0;i<=n;i++){
		if(i<=r) p[i]=min(z[i-l+1],r-i+1);
		while(i+p[i]<=n&&a[i+p[i]]==b[p[i]+1]) p[i]++;
		if(i+p[i]-1>r) r=i+p[l=i]-1;
	}
}
int main(){
//	#ifdef LOCAL
//	 	freopen("input.in","r",stdin);
//	#endif
	scanf("%s%s",a+1,b+1),n=strlen(a+1),m=strlen(b+1);
	getz(b,m),match(a,n,b,m);
	LL ans1=0,ans2=0;
	for(int i=1;i<=n;i++) ans1^=1ll*i*(p[i]+1);
	for(int i=1;i<=m;i++) ans2^=1ll*i*(z[i]+1);
	printf("%lld\n%lld\n",ans2,ans1);
	return 0;
}

标签:函数,int,ir,char,20000010,KMP,include,模板
From: https://www.cnblogs.com/caijianhong/p/16863432.html

相关文章

  • 【模板】Tarjan
    postedon2022-07-0720:52:49|under模板|source0x00有向图缩点现有一有向图\(G=(V,E)\),称一个点集\(E'\inE\)为强连通分量,当且仅当\(E'\)的任意两点可以互......
  • 【模板】ST 表 Sparse Table
    postedon2022-07-2219:15:58|under模板|sourcetemplate<intN,classT=int,intlogN=20>structSTable{ inttot,lg[N+10];Tf[logN+1][N+10]; STable():tot(0......
  • 【模板】Splay
    postedon2022-07-2117:03:54|under模板|source(介绍等会补)调试:getpre、getsuf、find手写,常数不要乘以二。UB:getkth和getrnk叠起来的时候root会改!UB:getp......
  • 【模板】popcount
    postedon2022-02-0418:11:33|under模板|sourceintpopcount(intx){#defineBIT2(n)n,n+1,n+1,n+2#defineBIT4(n)BIT2(n),BIT2(n+1),BIT2(n+1),BIT2......
  • 【模板】Bellman-Ford 判负环
    postedon2022-08-1018:07:25|under模板|source0x00Bellman-Ford最短路经过的边数不超过\(n-1\),因此若松弛轮数达到\(n\)轮即有负环。复杂度\(O(nm)\)。int......
  • 【模板】AC 自动机
    postedon2022-06-1111:17:10|under模板|sourcetemplate<intN,intM=26,intD='a'>structacam{ intch[N+10][M],cnt[N+10],tot,fail[N+10]; intsum[N+10],i......
  • 【模板】01-trie
    postedon2022-01-1716:13:39|under模板|source#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;template<intN,intM=2,intM......
  • webstorm里面react快速创建模板
    webstorm里面react快速创建模板rcc+tab键--用ES6模块系统创建一个React组件类rccp+tab键--创建一个带有PropTypes和ES6模块系统的React组件类rcfc+tab键-......
  • [代码审计]信呼协同办公系统2.2存在文件上传配合云处理函数组合拳RCE
    文章目录​​写在前面​​分析​​​​脚本​​写在前面本次强网杯决赛的一个题,还是蛮有意思的,代码可以在github拿到​​​https://github.com/rainrocka/xinhu​​分析首......
  • 指针+函数解决问题
    问题:交换两个整形的数值#include<stdio.h>voidswap(int*pa, int*pb){inttmp=*pa;*pa=*pb;*pb=tmp;}int main(){int a=45;int b=86;printf("a=%db=%d\n",a,b);sw......