Submission #238153

# Submission time Handle Problem Language Result Execution time Memory
238153 2020-06-10T05:21:18 Z eggag32 Palindromes (APIO14_palindrome) C++17
23 / 100
235 ms 131076 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) debug(x) debug(y);
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end() 
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

pair<ll, ll> hsh[10005];
ll inv1[10005], inv2[10005];
ll p = 349, p1 = 179, m = 1e9 + 7, m1 = 1e9 + 9;
 
void computeHashes(string s){
	ll hash_value1 = 0, hash_value2 = 0;
	ll p_pow1 = 1, p_pow2 = 1;
	int cnt = 0;
	for(char c : s){
		hash_value1 = (hash_value1 + (c - '0' + 1) * p_pow1) % m;
		hash_value2 = (hash_value2 + (c - '0' + 1) * p_pow2) % m1;
		hsh[cnt] = mp(hash_value1, hash_value2);
		cnt++;
		p_pow1 = (p_pow1 * p) % m;
		p_pow2 = (p_pow2 * p1) % m1;
	}
}
 
ll mypow(ll a, ll p, ll MOD){
	if(p == 0) return 1;
	if(p == 1) return (a % MOD);
	if(p & 1) return ((a % MOD) * (mypow(a, p - 1, MOD) % MOD)) % MOD;
	ll x = mypow(a, p / 2, MOD) % MOD;
	return (x * x) % MOD;
}
 
void computeInvs(){
	inv1[0] = 1;
	for(int j = 1; j <= 10000; j++){
      		if(j == 1) inv1[j] = mypow(p, m - 2, m);
		else inv1[j] = (inv1[j - 1] * inv1[1]) % m;
	}
	inv2[0] = 1;
	for(int j = 1; j <= 10000; j++){
      		if(j == 1) inv2[j] = mypow(p1, m1 - 2, m1);
		else inv2[j] = (inv2[j - 1] * inv2[1]) % m1;
	}
}
 
pair<ll, ll> comp_hs(int i, int j){
	ll hs1 = ((hsh[j].fi - (i ? hsh[i - 1].fi : 0)) + m) % m;
	ll hs2 = ((hsh[j].se - (i ? hsh[i - 1].se : 0)) + m1) % m1;
	(hs1 *= inv1[i]) %= m;
	(hs2 *= inv2[i]) %= m1;
	return mp(hs1, hs2);
}

int pal[10005][10005]; //is s[i : j] a palindrome?

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	string s;
	cin >> s;
	int n = s.size();
	computeInvs();
	computeHashes(s);
	rep(i, n) pal[i][i] = 1;
	rep(i, n - 1) if(s[i] == s[i + 1]) pal[i][i + 1] = 1;
	repn(i, 2, n){
		rep(j, n - i + 1){
			if(s[j] == s[j + i] && pal[j + 1][j + i - 1]) pal[j][j + i] = 1;
		}
	}
	map<pair<ll, ll>, pi> m;
	rep(i, n) repn(j, i, n) if(pal[i][j]){
		pair<ll, ll> cr = comp_hs(i, j);
		m[cr].fi = (j - i + 1);
		m[cr].se++;
	}	
	ll ans = 0;
	for(pair<pair<ll, ll>, pi> x : m){
		ans = max(ans, (ll)(x.se.se * x.se.fi));
	}
	cout << ans << endl;	
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Array bounds
	- Special cases
Be careful!
*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 6 ms 512 KB Output is correct
11 Correct 5 ms 640 KB Output is correct
12 Correct 5 ms 640 KB Output is correct
13 Correct 5 ms 640 KB Output is correct
14 Correct 5 ms 640 KB Output is correct
15 Correct 5 ms 640 KB Output is correct
16 Correct 5 ms 640 KB Output is correct
17 Correct 5 ms 640 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 5 ms 896 KB Output is correct
20 Correct 6 ms 896 KB Output is correct
21 Correct 5 ms 896 KB Output is correct
22 Correct 5 ms 896 KB Output is correct
23 Correct 5 ms 896 KB Output is correct
24 Correct 5 ms 896 KB Output is correct
25 Correct 5 ms 896 KB Output is correct
26 Correct 5 ms 896 KB Output is correct
27 Correct 5 ms 896 KB Output is correct
28 Correct 5 ms 896 KB Output is correct
29 Correct 5 ms 896 KB Output is correct
30 Correct 5 ms 896 KB Output is correct
31 Correct 5 ms 896 KB Output is correct
32 Correct 5 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 6528 KB Output is correct
2 Correct 18 ms 5504 KB Output is correct
3 Correct 65 ms 6528 KB Output is correct
4 Correct 10 ms 4864 KB Output is correct
5 Correct 62 ms 6528 KB Output is correct
6 Correct 65 ms 6648 KB Output is correct
7 Correct 9 ms 5888 KB Output is correct
8 Correct 37 ms 6528 KB Output is correct
9 Correct 9 ms 4864 KB Output is correct
10 Correct 8 ms 4608 KB Output is correct
11 Correct 8 ms 4608 KB Output is correct
12 Correct 10 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 235 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 1536 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 2696 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -