제출 #545191

#제출 시각아이디문제언어결과실행 시간메모리
545191aryan12Palindromic Partitions (CEOI17_palindromic)C++17
100 / 100
4761 ms34764 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e6 + 5, PRIME = 566376197, MOD = 1e9 + 9;
int hash_constant[N], hash_value[N], pref_hash_value[N];
string s;

int power(int a, int b) {
	if(b == 0) {
		return 1;
	}
	if(b == 1) {
		return a % MOD;
	}
	int x = power(a, b / 2);
	x *= x;
	x %= MOD;
	if(b & 1) {
		x *= a;
		x %= MOD;
	}
	return x;
}

int CalcHashValue(int l, int r) {
	int inv = power(power(PRIME, l), MOD - 2);
	int lmao = pref_hash_value[r] - pref_hash_value[l - 1];
	if(lmao < 0) {
		lmao += MOD;
	}
	lmao %= MOD;
	int ans = (lmao * inv) % MOD;
	return ans;
}

int F(int l, int r) {
	if(l > r) {
		return 0;
	}
	if(l == r) {
		return 1;
	}
	int i = l, j = r;
	while(i < j) {
		if(CalcHashValue(l, i) == CalcHashValue(j, r)) {
			return F(i + 1, j - 1) + 2;
		}
		i++, j--;
	}
	return 1;
}

void Solve() {
	cin >> s;
	int n = s.size();
	pref_hash_value[0] = 0;
	for(int i = 1; i <= n; i++) {
		hash_value[i] = (power(PRIME, i) * (s[i - 1] - 'a' + 1)) % MOD;
		pref_hash_value[i] = pref_hash_value[i - 1] + hash_value[i];
		pref_hash_value[i] %= MOD;
		//cout << hash_value[i] << " " << pref_hash_value[i] << "\n";
	}
	cout << F(1, n) << "\n";
}

int32_t main() {
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	for(int i = 0; i < N; i++) {
		hash_constant[i] = power(PRIME, i);
	}
	int t = 1;
	cin >> t;
	while(t--) {
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\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...