73分暴力(无二分)哈希
查看原帖
73分暴力(无二分)哈希
689640
Michael2012楼主2024/10/17 19:27
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5e3 + 5, P = 179;
typedef unsigned long long ULL;
struct node {
	int l, r;
	ULL v;
} b[N];
int n, a[N] ,  cnt;
ULL p[N], c[N];
bool operator < (const node &x, const node &y) {
	return x.v < y.v || x.v == y.v && x.l < y.r;
}
ULL Hash(int x, int y) {
	return c[y] - c[x - 1] * p[y - x + 1];
}
int main() {
	cin >> n;
	p[0] = 1;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		p[i] = p[i - 1] * P;
	}
	for (int i = 1; i < n; i++) {
		c[i] = c[i - 1] * P + a[i] - a[i + 1];
	}
	int ans = 0;
	for (int l = (n - 1) / 2;l >= 4;l--){
		cnt = 0;
		for (int i = 1;i + l - 1 < n;i++){
			int j = i + l - 1;
			b[++cnt] = {i , j , Hash(i , j)};
		}
		sort(b + 1 , b + cnt + 1);
		for (int i = 2;i <= cnt;i++){
		    /*
		    b[i - 1].l ~ b[i - 1].r + 1
		    b[i].l ~ b[i].r + 1
		    */
			if (b[i].v != b[i - 1].v || b[i].l <= b[i - 1].r + 1) continue;
			cout << l + 1 << '\n';
			return 0;
		}
	}
	cout << 0 << '\n';
	return 0;
}
2024/10/17 19:27
加载中...