Cod sursa(job #468121)

Utilizator vladiiIonescu Vlad vladii Data 2 iulie 2010 12:55:28
Problema Numarare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <iostream>
using namespace std;
#define nmax 100010

int N, D[nmax], P[nmax], L[nmax];
long long sol;

void palindrom() {
    int i, j, mxx;
    L[1] = 0; mxx = 1;
    for(i=2; i<=N; i++) {
         if(i < mxx + L[mxx]) {
              L[i] = min(L[mxx - (i - mxx)], mxx + L[mxx] - i);
         }
         
         if(mxx + L[mxx] <= i + L[i]) {
              mxx = i;
              while(1 <= i - L[i] - 1 && i + L[i] + 1 <= N && D[i + L[i] + 1] == D[i - L[i] - 1]) {
                   L[i]++;
              }
         }
    }     
}         

int main() {
    FILE *f1=fopen("numarare.in", "r"), *f2=fopen("numarare.out", "w");
    int i, j, p, q;
    fscanf(f1, "%d\n", &N);
    for(i=1; i<=N; i++) {
         fscanf(f1, "%d", &P[i]);
    }
    for(i=1; i<N; i++) {
         D[i] = P[i] - P[i + 1];
    }
    N = N - 1;
    
    palindrom();
    
    for(i=1; i<=N; i++) {
         sol += (L[i] + 1);
    }
    
    fprintf(f2, "%lld\n", sol);
    fclose(f1); fclose(f2);
    return 0;
}