Cod sursa(job #1570661)

Utilizator ArrowBasseStefan ArrowBasse Data 16 ianuarie 2016 18:46:07
Problema PScPld Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;




int get_number_of_palindroms_from(int x, const char *str, int l)
{
    //verifcam secventele centrate in x, apoi centrate in (x si x-1)
    int number_pal=1, left=x, right=x;

    //pt cenrare in x
    while(1)
    {
        left--;
        right++;
        if(right==l || left<0)
            break;
        if(str[left]==str[right])
            number_pal++;
        else
            break;
    }

    //pt centrare in x-1 si x
    left=x;
    right=x;


     //mia intai tb ca str[x-1] si str[x] sa fie egale
    left--;
    if(str[x]==str[left])
    {
        number_pal++;
        while(1)
        {
            left--;
            right++;
            if(right==l || left<0)
                break;
            if(str[left]==str[right])
                number_pal++;
            else
                break;
        }

    }



    return number_pal;


}

int main()
{
    fstream f, g;
    char str[1000000];
    int i, l, sum=0;
    f.open("pscpld.in", ios::in);
    g.open("pscpld.out", ios::out);
    f>>str; l=strlen(str);
    for(i=0; i<l; i++)
    {
        sum+=get_number_of_palindroms_from(i, str, l);
    }
    g<<sum;

    return 0;
}