Cod sursa(job #1503797)

Utilizator PenaluPenalu Ion Penalu Data 16 octombrie 2015 23:05:40
Problema PScPld Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 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

string t,s;
int pal[1000010];

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

	pal[0] = 0;
	int bst = 0;

	long long ans = 0;
	for (int i = 1; i < t.length(); ++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] < t.length() && 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)/2;
		else ans += 1 + pal[i]/2;
	}
	cout << ans;
}