Submission #399113

#TimeUsernameProblemLanguageResultExecution timeMemory
399113BERNARB01Palindromes (APIO14_palindrome)C++17
23 / 100
1086 ms8452 KiB
#include <bits/stdc++.h>

using namespace std;

void Z(const string& s, vector<int>& z) {
		z.assign(s.size(), 0);
    int n = s.size();
    int x = 0, y = 0;
    for (int i = 1; i < n; i++) {
        z[i] = max(0, min(z[i-x], y-i+1));
        while (i+z[i] < n && s[z[i]] == s[i+z[i]]) {
            x = i; y = i+z[i]; z[i]++;
        }
    }
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//freopen("palindrome.in", "r", stdin);
	//freopen("palindrome.out", "w", stdout);
	string s;
	cin >> s;
	vector<int> z;
	int n = s.length();
	long long res = 0;
	for (int i = 0; i < n; i++) {
		string t = s.substr(i, n - i);
		string rt = t;
		reverse(rt.begin(), rt.end());
		string t2 = t;
		t2 += '#';
		t2 += rt;
		Z(t2, z);
		bitset<1000> pal(0);
		for (int j = 0; j < (int) z.size(); j++) {
			if (z[z.size() - j - 1] == j + 1) {
				pal[j] = 1;
			}
		}
		t += '#';
		t += s;
		Z(t, z);
		vector<long long> cnt(n + 1, 0);
		for (int j = n - i; j < (int) z.size(); j++) {
			cnt[z[j]]++;
		}
		for (int j = n - 1; j >= 0; j--) {
			cnt[j] += cnt[j + 1];
		}
		for (int j = i; j < n; j++) {
			long long len = j - i + 1;
			if (!pal[j - i]) {
				continue;
			}
			long long ans = cnt[len];
			ans *= len;
			res = max(res, ans);
		}
	}
	cout << res << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...