Cod sursa(job #1503806)

Utilizator PenaluPenalu Ion Penalu Data 16 octombrie 2015 23:13:51
Problema PScPld Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <cstring>
#include <map>
#include <unordered_set>
using namespace std;

#define sq 300
#define maxn 100010

int pal[2000010];
char t[2000010], s[1000010];

int main()
{
	freopen("pscpld.in","r",stdin);
	freopen("pscpld.out","w",stdout);
	//ios_base::sync_with_stdio(false);
	cin >> s;
	int m = strlen(s);
	t[0] = '0';
	for (int i = 0; i < m; ++i)
	{
		t[(i<<1)+1] = s[i];
		t[(i<<1)+2] = '0';
	}

	int n = strlen(t);
	pal[0] = 0;
	int bst = 0;

	long long ans = 0;
	for (int i = 1; i < n; ++i)
	{
		if (bst+pal[bst] < i || pal[bst-(i-bst)] >= bst+pal[bst] - i)
		{
			bst = i;
			if(bst-(i-bst) >= 0)
				pal[i] = pal[bst-(i-bst)];
			while (i+pal[i] < n && t[i+pal[i]] == t[i-pal[i]])
				++pal[i];
			--pal[i];
		}
		else
		{
			pal[i] = pal[bst-(i-bst)];
		}
		if (t[i] == '0')
			ans += (pal[i]+1)>>1;
		else ans += 1 + pal[i]>>1;
	}
	cout << ans;
}