Cod sursa(job #468427)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 3 iulie 2010 14:59:40
Problema PScPld Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <cstdio>
#include <cstring>
const int NMAX=1000005;
char s[NMAX];
int L[2*NMAX];
inline int min(int x,int y) { return x<y?x:y; }
int main()
{
	freopen("pscpld.in","r",stdin);
	fgets(s,NMAX,stdin);
	int N=strlen(s)-1,i,j,p,beg,end,q;

	L[1]=1;beg=end=0;
	if (N>1 && s[0]==s[1]) L[0]=2,beg=0,end=1;

	for (i=1;i<N;++i)
	  if (i<=end)
	  {
		j=beg+end-i;
		L[2*i+1]=min(L[2*j+1],(end-i)*2 + 1);
		if (L[2*i+1]==(end-i)*2 + 1)
		{
			for (p=end-L[2*i+1],q=end+1;q<N && p>=0 && s[q]==s[p];--p,++q) L[2*i+1]+=2;
			beg=p+1;end=q-1;
		}
		j=beg+end-i-1;
		if (j>=beg) L[2*i]=min(L[2*j],(end-i)*2);
		p=i-(L[2*i]/2);q=i+(L[2*i]/2)+1;
		while (p>=0 && q<N && s[p]==s[q]) L[2*i]+=2,--p,++q;
		++p;--q;
		if (q>end) beg=p,end=q;
	  }
	  else
	  {
		  L[2*i+1]=1;
		  for (p=i-1,q=i+1;p>=0 && q<N && s[p]==s[q];--p,++q) L[2*i+1]+=2;
		  beg=p+1;end=q-1;
		  j=beg+end-i-1;
		  if (j>=beg) L[2*i]=min(L[2*j],(end-i)*2);
		  p=i-(L[2*i]/2);q=i+1+(L[2*i]/2);
		  while (p>=0 && q<N && s[p]==s[q]) L[2*i]+=2,--p,++q;
		  ++p;--q;
		  if (q>end) beg=p,end=q;
	  }

	long long sol=0;
	for (i=0;i<N;++i) sol+=(L[2*i] + L[2*i+1]+1)/2;
	freopen("pscpld.out","w",stdout);
	printf("%lld\n",sol);


	return 0;
}