Cod sursa(job #6582)

Utilizator MariusMarius Stroe Marius Data 20 ianuarie 2007 12:05:55
Problema PScPld Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <cstdio>
#include <string>
using namespace std;

const char iname[] = "pscpld.in";
const char oname[] = "pscpld.out";

#define MAX_N 1000007

char A[MAX_N];

int  L[2 * MAX_N], N;

long long Res;


void read_in(void) {
	freopen(iname, "r", stdin);
	scanf("%s", A + 1);
	N = int(strlen(A + 1));
}

int main(void) {
	read_in();
	
	for (int index = 1; index < N << 1; ++index) {
		if ((index & 1) && (L[index] == 0))
			L[index] = 1;
		int li = index - L[index] - 1;
		int ls = index + L[index] + 1;

		if (index & 1) {
			if ((ls < N << 1) == 0) {
				L[index] = (((N << 1) - index) / 2) * 2 + 1;
				continue ;
			}				
			for (; (0 < li) && (ls < N << 1) && (A[li / 2 + 1] == A[ls / 2 + 1]); li -= 2, ls += 2) {
				if (L[ls - 1] < L[li + 1])
					L[ls - 1] = L[li + 1];
				if (L[ls] < L[li])
					L[ls] = L[li];
				L[index] += 2;
			}
		} else {
			if ((ls < N << 1) == 0) {
				L[index] = (N << 1) - index;
				continue ;
			}
			for (; (0 < li) && (ls < N << 1) && (A[li / 2 + 1] == A[ls / 2 + 1]); li -= 2, ls += 2) {
				if (L[ls - 1] < L[li + 1])
					L[ls - 1] = L[li + 1];
				if (L[ls] < L[li])
					L[ls] = L[li];
				L[index] += 2;
			}
		}
	}
	for (int index = 1; index < N << 1; ++index) {
		if (index & 1)
			Res += (long long) (L[index] / 2 + 1);
		else
			Res += (long long) (L[index] / 2);
	}
	freopen(oname, "w", stdout);
	printf("%lld\n", Res);
	return 0;
}