Submission #239632

# Submission time Handle Problem Language Result Execution time Memory
239632 2020-06-16T22:10:34 Z eohomegrownapps Palindromic Partitions (CEOI17_palindromic) C++14
100 / 100
503 ms 35816 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll m = 1000000007;
ll p = 31;
ll pows[1000005];
void run(){
	string s;
	cin>>s;
	ll n= s.size();
	vector<ll> arr(n);
	for (ll i = 0; i<n; i++){
		arr[i]=s[i]-'a'+1;
	}
	vector<ll> ltor(n);//1,x+1,x^2+x+1...
	//vector<ll> rtol(n);//...x^2+x+1,x+1,1
	ltor[0]=arr[0];
	for (ll i = 1; i<n; i++){
		ltor[i]=(ltor[i-1]*p+arr[i])%m;
	}
	/*rtol[n-1]=arr[n-1];
	for (ll i = n-2; i>=0; i--){
		rtol[i]=(rtol[i+1]*p+arr[i])%m;
	}*/
	ll cnt = 0;
	ll prevstart = 0;
	for (ll i = 0; i<n; i++){
		//check if prevstart to i forms a block
		ll pref = ltor[i];
		ll sub = prevstart==0?0:(ltor[prevstart-1]*pows[i-prevstart+1])%m;
		ll fwd = (pref-sub+m)%m;

		ll bkstart = n-1-i;
		ll bkend = n-1-prevstart;
		pref = ltor[bkend];
		sub = bkstart==0?0:(ltor[bkstart-1]*pows[bkend-bkstart+1])%m;
		ll bwd = (pref-sub+m)%m;
		if (fwd==bwd){
			cnt+=1;
			prevstart=i+1;
		}
	}
	cout<<cnt<<'\n';
}

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	pows[0]=1;
	for (ll i = 1; i<1000005; i++){
		pows[i]=(pows[i-1]*p)%m;
	}
	ll n;
	cin>>n;
	for (ll i = 0; i<n; i++){
		run();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 8192 KB Output is correct
2 Correct 14 ms 8192 KB Output is correct
3 Correct 14 ms 8192 KB Output is correct
4 Correct 15 ms 8192 KB Output is correct
5 Correct 15 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 8192 KB Output is correct
2 Correct 14 ms 8192 KB Output is correct
3 Correct 14 ms 8192 KB Output is correct
4 Correct 15 ms 8192 KB Output is correct
5 Correct 15 ms 8192 KB Output is correct
6 Correct 14 ms 8192 KB Output is correct
7 Correct 14 ms 8192 KB Output is correct
8 Correct 14 ms 8192 KB Output is correct
9 Correct 14 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 8192 KB Output is correct
2 Correct 14 ms 8192 KB Output is correct
3 Correct 14 ms 8192 KB Output is correct
4 Correct 15 ms 8192 KB Output is correct
5 Correct 15 ms 8192 KB Output is correct
6 Correct 14 ms 8192 KB Output is correct
7 Correct 14 ms 8192 KB Output is correct
8 Correct 14 ms 8192 KB Output is correct
9 Correct 14 ms 8192 KB Output is correct
10 Correct 18 ms 8476 KB Output is correct
11 Correct 16 ms 8404 KB Output is correct
12 Correct 18 ms 8456 KB Output is correct
13 Correct 19 ms 8432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 8192 KB Output is correct
2 Correct 14 ms 8192 KB Output is correct
3 Correct 14 ms 8192 KB Output is correct
4 Correct 15 ms 8192 KB Output is correct
5 Correct 15 ms 8192 KB Output is correct
6 Correct 14 ms 8192 KB Output is correct
7 Correct 14 ms 8192 KB Output is correct
8 Correct 14 ms 8192 KB Output is correct
9 Correct 14 ms 8192 KB Output is correct
10 Correct 18 ms 8476 KB Output is correct
11 Correct 16 ms 8404 KB Output is correct
12 Correct 18 ms 8456 KB Output is correct
13 Correct 19 ms 8432 KB Output is correct
14 Correct 460 ms 35816 KB Output is correct
15 Correct 246 ms 30380 KB Output is correct
16 Correct 503 ms 34152 KB Output is correct
17 Correct 231 ms 23048 KB Output is correct