Pagini recente » Cod sursa (job #357362) | Cod sursa (job #948915) | Cod sursa (job #2378406) | Cod sursa (job #2332706) | Cod sursa (job #2791659)
#include <iostream>
#include <cstring>
#include<fstream>
using namespace std;
#define MAX 1000000
//alg lui manacher
ifstream f ("pscpld.in");
ofstream g ("pscpld.out");
char sir[2*MAX+3];
int rasp[2*MAX+3];
long long cnt = 1,rasp_final;
int centru_palindrom,pozitie_maxima_palindrom;
int main()
{
char c;
sir[0] = '#';
while(f>>c)
{
sir[cnt++] = c;
sir[cnt++] = '#';
}
int l = strlen(sir);
for(int i =0;i<l;i++)
{
int simetric_fata_de_centru_palindrom = centru_palindrom - (i-centru_palindrom);
if(i<pozitie_maxima_palindrom)
{
rasp[i] = min(pozitie_maxima_palindrom-i,rasp[simetric_fata_de_centru_palindrom]);
}
while(i-1-rasp[i]>=0 && i +1+rasp[i]<l && sir[i+1+rasp[i]]==sir[i-1-rasp[i]])
rasp[i]++;
if(rasp[i]+i>pozitie_maxima_palindrom)
{
pozitie_maxima_palindrom = i+rasp[i];
centru_palindrom = i;
}
rasp_final +=rasp[i]/2 + rasp[i]%2;
}
g << rasp_final;
}