求助z函数TLE成了8分
查看原帖
求助z函数TLE成了8分
347589
Zelotz楼主2022/11/2 13:23

RT 求助为什么

#include <bits/stdc++.h>
using namespace std;
#define srand srand(time(NULL))
#define random(x) rand() % (x)
#define il inline
#define ptc putchar
#define pb push_back
#define reg register
#define mp make_pair
#define R(i, l, r) for (int i = l; i <= r; ++i)
#define debug puts("--------------------------------------------")
typedef __int128 LL;
typedef long long ll;
typedef pair<int, int> PII;
namespace HOOOOOCH {
	template <typename T>
	il void read(T &x) {
		x = 0; T f = 1; char ch;
		while (!isdigit(ch = getchar())) f -= (ch == '-') << 1;
		while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch & 15), ch = getchar(); x *= f;
	}
	template <typename T, typename ...L>
	il void read(T &x, L &...y) {read(x); read(y...);}
	template <typename T>
	il void write(T x) {
		if (x < 0) ptc('-'), x = -x;
		if (x > 9) write(x / 10);
		ptc(x % 10 + '0');
	}
	template <typename T, typename ...L>
	il void write(T &x, L &...y) {write(x), ptc(' '); write(y...);}
}
using namespace HOOOOOCH;
const int N = 2e7 + 5;
string a, b;
int z[N];
void getz() {
	int l = 0, r = 0, n = b.size();
	z[0] = n;
	for (int i = 1; i < n; ++i) {
		if (i <= r) {
			if (z[i - l] < r - i + 1) z[i] = z[i - l];
			else {
				z[i] = r - i + 1;
				while (i + z[i] < n && b[z[i]] == b[i + z[i]]) ++z[i];
			}
		}
		else while (i + z[i] < n && b[z[i]] == b[i + z[i]]) ++z[i];
//		cout << z[i] << endl;
	}
	int res = 0; 
	R(i, 0, n - 1) res ^= (i + 1) * (z[i] + 1);
	write(res), ptc('\n');
}
void exkmp() {
	memset(z, 0, sizeof z); // z[i] 表示a[i~n-1]与b的最长lcp长度 
	int m = a.size(), n = b.size();
	R(i, 0, min(n - 1, m - 1)) {
		if (a[i] == b[i]) ++z[0];
		else break;
	}
	int l = 0, r = 0;
	for (int i = 1; i < m; ++i) {
		if (i <= r) {
			if (z[i - l] < r - i + 1) z[i] = z[i - l];
			else {
				z[i] = r - i + 1;
				while (i + z[i] < m && z[i] < n && b[z[i]] == a[i + z[i]]) ++z[i];
			}
		}
		else while (i + z[i] < m && z[i] < n && b[z[i]] == a[i + z[i]]) ++z[i];
	}
	int res = 0;
	R(i, 0, m - 1) res ^= (i + 1) * (z[i] + 1);
	write(res), ptc('\n');
}
signed main() {
//	freopen("P5410_2.in", "r", stdin);
	cin >> a >> b;
	getz(); exkmp();
	return 0;
}
2022/11/2 13:23
加载中...