Submission #670226

# Submission time Handle Problem Language Result Execution time Memory
670226 2022-12-08T10:33:40 Z Radin_Zahedi2 Palindromes (APIO14_palindrome) C++17
50 / 100
1000 ms 31748 KB
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
const int mod = 1e9 + 7;
const int p = 2;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;


int n;
string s;
const int N = 3e5 + 5;
int hashed[N];
int hsahed[N];
int po[N];

int a[N];
int cl[N];
int lcp[N];

int par[N];
int len[N];


int mull(int a, int b) {
	return (1ll * a * b) % mod;
}

void cntsort() {
	vector<int> have[n];

	for (int i = 0; i < n; i++) {
		have[cl[a[i]]].pb(a[i]);
	}

	int ind = 0;
	for (int i = 0; i < n; i++) {
		for (auto x : have[i]) {
			a[ind] = x;
			ind++;
		}
	}
}

void cresuff() {
	pair<char, int> arr[n];
	for (int i = 0; i < n; i++) {
		arr[i] = mp(s[i], i);
	}
	sort(arr + 0, arr + n);

	a[0] = arr[0].se;
	cl[a[0]] = 0;
	for (int i = 1; i < n; i++) {
		a[i] = arr[i].se;

		if (arr[i].fi == arr[i - 1].fi) {
			cl[a[i]] = cl[a[i - 1]];
		}
		else {
			cl[a[i]] = cl[a[i - 1]] + 1;
		}
	}


	int len = 1;
	while (len < n) {
		for (int i = 0; i < n; i++) {
			a[i] -= len;
			a[i] += n;
			a[i] %= n;
		}

		cntsort();

		int cl2[n];

		cl2[a[0]] = 0;
		for (int i = 1; i < n; i++) {
			pair<int, int> pre = mp(cl[a[i - 1]], cl[(a[i - 1] + len) % n]);
			pair<int, int> now = mp(cl[a[i]], cl[(a[i] + len) % n]);

			if (pre == now) {
				cl2[a[i]] = cl2[a[i - 1]];
			}
			else {
				cl2[a[i]] = cl2[a[i - 1]] + 1;
			}
		}

		for (int i = 0; i < n; i++) {
			cl[i] = cl2[i];
		}

		len <<= 1;
	}
}

void crelcp() {
	int have = 0;

	for (int i = 0; i < n - 1; i++) {
		int ind = cl[i];

		while (i + have < n && a[ind - 1] + have < n) {
			if (s[i + have] != s[a[ind - 1] + have]) {
				break;
			}

			have++;
		}

		lcp[ind] = have;

		have = max(0, have - 1);
	}
}

void crehash() {
	po[0] = 1;
	for (int i = 1; i <= n; i++) {
		po[i] = mull(po[i - 1], p);
	}

	hashed[0] = (int)s[0];
	for (int i = 1; i < n; i++) {
		hashed[i] = mull(hashed[i - 1], p);
		hashed[i] += (int)s[i];
	}

	hsahed[n - 1] = (int)s[n - 1];
	for (int i = n - 2; i >= 0; i--) {
		hsahed[i] = mull(hsahed[i + 1], p);
		hsahed[i] += (int)s[i];
	}
}

bool palin(int l, int r) {
	int val;
	if (l == 0) {
		val = hashed[r];
	}
	else {
		val = hashed[r] - mull(po[r - l + 1], hashed[l - 1]);
		val += mod;
		val %= mod;
	}

	int lav;
	if (r == n - 1) {
		lav = hsahed[l];
	}
	else {
		lav = hsahed[l] - mull(po[r - l + 1], hsahed[r + 1]);
		lav += mod;
		lav %= mod;
	}

	return (val == lav);
}

void init() {
}

void input() {
	cin >> s;
	n = sz(s);

	n++;
	s += '$';
}

void solve() {
	cresuff();
	crelcp();

	crehash();

	for (int i = 0; i < n; i++) {
		par[i] = i;
		len[i] = 1;
	}

	vector<pair<int, int>> edges;
	for (int i = 1; i < n; i++) {
		edges.pb(mp(lcp[i], i));
	}

	sort(edges.begin(), edges.end());
	reverse(edges.begin(), edges.end());

	ll ans = 0;

	if (palin(0, n - 2)) {
		ans = n - 1;
	}
	for (int i = 0; i < n - 1; i++) {
		int ind = edges[i].se;

		int p1 = par[ind - 1];
		int p2 = par[ind];

		if (palin(a[ind], a[ind] + lcp[ind] - 1)) {
			ans = max(ans, 1ll * lcp[ind] * (len[p1] + len[p2]));
		}

		if (len[p1] < len[p2]) {	
			for (int i = ind - len[p1]; i < ind; i++) {
				par[i] = p2;
			}

			len[p2] += len[p1];
		}
		else {
			for (int i = ind; i <= ind + len[p2] - 1; i++) {
				par[i] = p1;
			}

			len[p1] += len[p2];
		}
	}

	cout << ans;
}

void output() {
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int number_of_testcases = 1;
	//cin >> number_of_testcases;
	while (number_of_testcases--) {
		init();

		input();

		solve();

		output();
	}

	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Incorrect 0 ms 340 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Incorrect 1 ms 468 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1344 KB Output is correct
2 Correct 6 ms 1316 KB Output is correct
3 Correct 6 ms 1284 KB Output is correct
4 Correct 6 ms 1204 KB Output is correct
5 Correct 8 ms 1364 KB Output is correct
6 Correct 8 ms 1424 KB Output is correct
7 Correct 7 ms 1364 KB Output is correct
8 Correct 11 ms 1336 KB Output is correct
9 Correct 11 ms 1364 KB Output is correct
10 Correct 8 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 8904 KB Output is correct
2 Correct 68 ms 9724 KB Output is correct
3 Correct 66 ms 8860 KB Output is correct
4 Correct 75 ms 9764 KB Output is correct
5 Correct 101 ms 10700 KB Output is correct
6 Correct 100 ms 10520 KB Output is correct
7 Correct 95 ms 10312 KB Output is correct
8 Correct 205 ms 10376 KB Output is correct
9 Correct 182 ms 10512 KB Output is correct
10 Correct 103 ms 10192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 244 ms 30056 KB Output is correct
2 Correct 253 ms 28768 KB Output is correct
3 Correct 256 ms 30196 KB Output is correct
4 Correct 252 ms 28748 KB Output is correct
5 Correct 390 ms 31504 KB Output is correct
6 Correct 376 ms 31156 KB Output is correct
7 Correct 442 ms 31748 KB Output is correct
8 Execution timed out 1064 ms 21716 KB Time limit exceeded
9 Halted 0 ms 0 KB -