Cod sursa(job #467388)

Utilizator AndreyPAndrei Poenaru AndreyP Data 28 iunie 2010 16:57:30
Problema PScPld Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <cstdio>
#define N 1000010
#define M 2000010
#define ll long long

char c[N];
char a[M];
int lun[M];

inline void rezolva()
{
	fgets(c+1,N-1,stdin);
	int n=1;
	a[1]='*';
	for(int i=1; c[i]>='a' && c[i]<='z'; ++i)
	{
		a[++n]=c[i];
		a[++n]='*';
	}
	
	lun[1]=0;
	int st=1,dr=1;
	int aux;
	ll rez=0;
	
	for(int i=2; i<=n; ++i)
	{
		if(i<=dr)
		{
			aux=dr-i;
			if(i+lun[st+aux]>=dr)
			{
				lun[i]=aux;
				st=i-lun[i];
				dr=i+lun[i];
				while(st>1 && dr<n && a[st-1]==a[dr+1])
				{
					--st;
					++dr;
					++lun[i];
				}
			}
			else
				lun[i]=lun[st+aux];
		}
		else
		{
			st=dr=i;
			lun[i]=0;
			while(st>1 && dr<n && a[st-1]==a[dr+1])
			{
				--st;
				++dr;
				++lun[i];
			}
		}
		
		if(i&1)
			rez+=(ll)((lun[i]>>1)+(lun[i]&1));
		else
			rez+=(ll)((lun[i]>>1)+1);
	}
	
	printf("%lld\n",rez);
}

int main()
{
	freopen("pscpld.in","r",stdin);
	freopen("pscpld.out","w",stdout);
	
	rezolva();
	
	return 0;
}