其他分享
首页 > 其他分享> > 【模板】后缀排序

【模板】后缀排序

作者:互联网

\(Code\)

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 2e7 + 5;
LL z[N],p[N]; char a[N],b[N];

int main()
{
	scanf("%s%s",a + 1,b + 1);
	int la = strlen(a + 1),lb = strlen(b + 1);
	for (int i = 2,l = 0,r = 0; i <= lb; i++)
	{
		z[i] = (i <= r ? min(z[i - l + 1],(LL)r - i + 1) : 0);
		while (i + z[i] <= lb && b[z[i] + 1] == b[z[i] + i]) z[i]++;
		if (i + z[i] - 1 > r) r = i + z[i] - 1,l = i;	
	}
	z[1] = lb; LL Z = 0,P = 0;
	for (int i = 1; i <= lb; i++) Z ^= (LL)i * (z[i] + 1LL);
	printf("%lld\n",Z);
	for (int i = 1,l = 0,r = 0; i <= la; i++)
	{
		p[i] = (i <= r ? min(z[i -l + 1],(LL)r - i + 1): 0);
		while (i + p[i] <= la && p[i] < lb && a[i + p[i]] == b[p[i] + 1]) p[i]++;
		if (i + p[i] - 1 > r) r = i + p[i] - 1,l = i;
	}
	for (int i = 1; i <= la; i++) P ^= (LL)i * (p[i] + 1LL);
	printf("%lld\n",P);
}

标签:include,lb,后缀,LL,long,int,排序,strlen,模板
来源: https://www.cnblogs.com/nibabadeboke/p/15922618.html