답안 #239630

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
239630 2020-06-16T22:08:33 Z eohomegrownapps Palindromic Partitions (CEOI17_palindromic) C++14
0 / 100
12 ms 4224 KB
#include <bits/stdc++.h>
using namespace std;

int m = 1000000007;
int p = 37;
int pows[1000005];
void run(){
	string s;
	cin>>s;
	int n= s.size();
	vector<int> arr(n);
	for (int i = 0; i<n; i++){
		arr[i]=s[i]-'a'+1;
	}
	vector<int> ltor(n);//1,x+1,x^2+x+1...
	//vector<int> rtol(n);//...x^2+x+1,x+1,1
	ltor[0]=arr[0];
	for (int i = 1; i<n; i++){
		ltor[i]=(ltor[i-1]*p+arr[i])%m;
	}
	/*rtol[n-1]=arr[n-1];
	for (int i = n-2; i>=0; i--){
		rtol[i]=(rtol[i+1]*p+arr[i])%m;
	}*/
	int cnt = 0;
	int prevstart = 0;
	for (int i = 0; i<n; i++){
		//check if prevstart to i forms a block
		int pref = ltor[i];
		int sub = prevstart==0?0:ltor[prevstart-1]*pows[i-prevstart+1];
		int fwd = (pref-sub+m)%m;

		int bkstart = n-1-i;
		int bkend = n-1-prevstart;
		pref = ltor[bkend];
		sub = bkstart==0?0:ltor[bkstart-1]*pows[bkend-bkstart+1];
		int 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 (int i = 1; i<1000005; i++){
		pows[i]=(pows[i-1]*p)%m;
	}
	int n;
	cin>>n;
	for (int i = 0; i<n; i++){
		run();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 4224 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 4224 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 4224 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 4224 KB Output isn't correct
2 Halted 0 ms 0 KB -