Pagini recente » Cod sursa (job #1418997) | Cod sursa (job #358583) | Cod sursa (job #1465333) | Cod sursa (job #1907954) | Cod sursa (job #1427024)
#include <bits/stdc++.h>
using namespace std;
const int MAX_LEN = 200000;
const int SIGMA = 26;
int str[MAX_LEN];
int length[MAX_LEN];
int link[MAX_LEN];
int value[MAX_LEN];
int to[MAX_LEN][SIGMA];
int lastNode;
int countNodes;
int lengthString;
void initTree()
{
lengthString = 0;
countNodes = 2;
lastNode = 0;
link[0] = 1;
length[1] = -1;
str[ lengthString++ ] = -1;
}
int getLink(int v)
{
while (str[lengthString - length[v] - 2] != str[lengthString - 1])
v = link[v];
return v;
}
void addLetter(int c)
{
str[ lengthString++ ] = c;
lastNode = getLink(lastNode);
if (!to[lastNode][c])
{
length[countNodes] = length[lastNode] + 2;
link[countNodes] = to[getLink(link[lastNode])][c];
to[lastNode][c] = countNodes++;
value[countNodes - 1] = value[ link[countNodes - 1] ] + 1;
}
lastNode = to[lastNode][c];
}
int main()
{
ifstream in("pscpld.in");
ofstream out("pscpld.out");
initTree();
char c;
long long sol = 0;
while (in >> c)
{
addLetter(c);
sol += value[lastNode];
}
out << sol << "\n";
return 0;
}