제출 #693685

#제출 시각아이디문제언어결과실행 시간메모리
693685PoPularPlusPlusPalindromic Partitions (CEOI17_palindromic)C++17
100 / 100
3031 ms11832 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long 
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007; 
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;

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

bool remender(ll a , ll b){return a%b;}

//freopen("problemname.in", "r", stdin);
//freopen("problemname.out", "w", stdout);

const int P = 31;

ll add(ll x, ll y){
	return (x+y)%mod;
}

ll multi(ll x , ll y){
	return (x*y)%mod;
}

ll power(ll x , ll y){
	x %= mod;
	ll res = 1;
	while(y > 0){
		if(y&1)res=multi(res,x);
		y=y>>1;
		x = multi(x,x);
	}
	return res;
}

ll inv(ll n){
	return power(n,mod-2);
}

void solve(){
	int n;
	string s;
	cin >> s;
	n = s.size();
	string a , b;
	a = "";
	b = "";
	for(int i = 0; i < n/2; i++)a += s[i];
	for(int i = (n + 1)/2; i < n; i++)b += s[i];
	int N = a.size();
	ll hash[N];
	ll hash1[N];
	for(int i = 0; i < N; i++){
		hash[i] = multi(power(P,i),a[i]-'a');
		if(i)hash[i] = add(hash[i],hash[i-1]);
		hash1[i] = multi(power(P,i),b[i]-'a');
		if(i)hash1[i] = add(hash1[i],hash1[i-1]);
	}
	int ans = 0;
	int i = 0;
	for(int j = 0; j < N; j++){
			ll x = hash[j] - (i > 0 ? hash[i-1] : 0);
			x += mod;
			x %= mod;
			x = multi(x , inv(power(P,i)));
			ll y = hash1[N-(i+1)];
			if(j < N - 1){
				y -= hash1[N-(j+2)];
				y += mod;
				y %= mod;
			}
			y = multi(y,inv(power(P,N-(j + 1))));
			//cout << i << ' ' << j << ' ' <<  x << ' ' << y << '\n';
			if(x == y){
				ans += 2;
				i = j + 1;
			}
		}
		if(n % 2 || i != N)ans++;
		cout << ans << '\n';
}	

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
	int t;cin >> t;while(t--)
	solve();
	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...