Submission #161020

# Submission time Handle Problem Language Result Execution time Memory
161020 2019-10-31T08:30:34 Z nvmdava Palindromic Partitions (CEOI17_palindromic) C++17
100 / 100
643 ms 58260 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define N 1000002
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007LL

ll MOD1 = 1000000207;
ll MOD2 = 1000000009;
int p = 29;

string s;

ll h1[N], h2[N];

ll po1[N], rpo1[N];
ll po2[N], rpo2[N];

ll binpow(ll a, ll b, ll m){
	ll s = 1;
	while(b){
		if(b & 1)
			s = s * a % m;
		b >>= 1;
		a = a * a % m;
	}
	return s;
}

pair<ll, ll> get(int l, int r){
	pair<ll, ll> res;
	res.ff = (h1[r] - h1[l - 1]) * rpo1[l] % MOD1;
	res.ss = (h2[r] - h2[l - 1]) * rpo2[l] % MOD2;
	if(res.ss < 0)
		res.ss += MOD2;
	if(res.ff < 0)
		res.ff += MOD1;
	return res;
}

void go(){
	cin>>s;
	int n = s.size();
	for(int i = 1; i <= n; i++){
		h1[i] = (h1[i - 1] + po1[i] * (s[i - 1] - 'a')) % MOD1;
		h2[i] = (h2[i - 1] + po2[i] * (s[i - 1] - 'a')) % MOD2;
	}

	int l = 1, r = n;
	int ans = 0;

	for(int i = 1; i < r; i++){
		if(get(l, i) == get(r - (i - l), r)){
			ans += 2;
			// cout<<l<<' '<<i<<' '<<r - (i - l)<<' '<<r<<'\n';
			r = r - (i - l) - 1;
			l = i + 1;
			if(l == r)
				break;
		}
	}

	if(l <= r)
		++ans;

	cout<<ans<<'\n';
}

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

    int t;
	h1[0] = 1;
	h2[0] = 1;
	po1[1] = po2[1] = rpo1[1] = rpo2[1] = 1;
	
	ll rp1, rp2;
	
	rp1 = binpow(p, MOD1 - 2, MOD1);
	rp2 = binpow(p, MOD2 - 2, MOD2);

	for(int i = 2; i < N; i++){
		po1[i] = po1[i - 1] * p % MOD1;
		po2[i] = po2[i - 1] * p % MOD2;
		rpo1[i] = rpo1[i - 1] * rp1 % MOD1;
		rpo2[i] = rpo2[i - 1] * rp2 % MOD2;
	}

	cin>>t;
    while(t--){
    	go();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 73 ms 31608 KB Output is correct
2 Correct 75 ms 31712 KB Output is correct
3 Correct 74 ms 31736 KB Output is correct
4 Correct 74 ms 31672 KB Output is correct
5 Correct 75 ms 31608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 31608 KB Output is correct
2 Correct 75 ms 31712 KB Output is correct
3 Correct 74 ms 31736 KB Output is correct
4 Correct 74 ms 31672 KB Output is correct
5 Correct 75 ms 31608 KB Output is correct
6 Correct 76 ms 31632 KB Output is correct
7 Correct 75 ms 31660 KB Output is correct
8 Correct 75 ms 31736 KB Output is correct
9 Correct 77 ms 31736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 31608 KB Output is correct
2 Correct 75 ms 31712 KB Output is correct
3 Correct 74 ms 31736 KB Output is correct
4 Correct 74 ms 31672 KB Output is correct
5 Correct 75 ms 31608 KB Output is correct
6 Correct 76 ms 31632 KB Output is correct
7 Correct 75 ms 31660 KB Output is correct
8 Correct 75 ms 31736 KB Output is correct
9 Correct 77 ms 31736 KB Output is correct
10 Correct 79 ms 31964 KB Output is correct
11 Correct 78 ms 31836 KB Output is correct
12 Correct 80 ms 31952 KB Output is correct
13 Correct 79 ms 31864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 31608 KB Output is correct
2 Correct 75 ms 31712 KB Output is correct
3 Correct 74 ms 31736 KB Output is correct
4 Correct 74 ms 31672 KB Output is correct
5 Correct 75 ms 31608 KB Output is correct
6 Correct 76 ms 31632 KB Output is correct
7 Correct 75 ms 31660 KB Output is correct
8 Correct 75 ms 31736 KB Output is correct
9 Correct 77 ms 31736 KB Output is correct
10 Correct 79 ms 31964 KB Output is correct
11 Correct 78 ms 31836 KB Output is correct
12 Correct 80 ms 31952 KB Output is correct
13 Correct 79 ms 31864 KB Output is correct
14 Correct 643 ms 58260 KB Output is correct
15 Correct 401 ms 53796 KB Output is correct
16 Correct 619 ms 57772 KB Output is correct
17 Correct 385 ms 45700 KB Output is correct