Cod sursa(job #1092860)

Utilizator apopeid14Apopei Daniel apopeid14 Data 27 ianuarie 2014 15:08:55
Problema PScPld Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <string.h>
 
using namespace std;
 
const char infile[] = "pscpld.in";
const char outfile[] = "pscpld.out";
 
ifstream fin(infile);
ofstream fout(outfile);
 
const int MAXN = 1000005;
const int oo = 0x3f3f3f3f;
 
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
 
const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }
 
char S[MAXN], M[2*MAXN];
int N, L;
int P[2*MAXN];
 
int main() {
    fin.getline(S, MAXN);
    L = strlen(S);
    M[ ++ N ] = '#';
    for(int i = 0 ; i < L ; ++ i) {
        M[ ++ N ] = S[i];
        M[ ++ N ] = '#';
    }
    long long Ans = 0;
    int best = 0, ind = 0;
    /// Manacher AlgoRITHM
    for(int i = 1 ; i <= N ; ++ i) {
        if(best >= i)
            P[i] = min(best - i, P[2 * ind - i]);
        while( i - P[i] - 1 >= 0 && i + P[i] + 1 <= N && M[i - P[i] - 1] == M[i + P[i] + 1])
            ++ P[i];
        if(i + P[i] > best) {
            best = i + P[i];
            ind = i;
        }
    }
    for(int i = 1 ; i <= N ; ++ i)
        Ans += (P[i] + 1) / 2;
    fout << Ans << '\n';
    fin.close();
    fout.close();
    return 0;
}