Cod sursa(job #636658)

Utilizator zloteanu.adrianzloteanu adrian nichita zloteanu.adrian Data 19 noiembrie 2011 22:20:26
Problema PalM Scor 10
Compilator cpp Status done
Runda .com 2011 Marime 2.38 kb
#include <cstdio>
#include <cstring>
#include <cctype>

bool isAlpha(char c)
{
    if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')))
    {
        return true;
    }
    return false;
}

bool isIdentic(char c1, char c2)
{
    if (tolower(c1) == tolower(c2))
    {
        return true;
    }
    return false;
}
int main()
{
    char originalString[501];
    char charsOnly[501];
    int charsPos[501];
    int orgStrSize = 0;
    int chrStrSize = 0;
    int foundMid;
    int foundSize = 0;
    bool sizeFlag;


    int maxM=-1;
    FILE *input = fopen("palm.in", "r");
    FILE *output = fopen("palm.out", "w");

    while (fscanf(input, "%c", &originalString[orgStrSize]) != EOF)
    {

        if (isAlpha(originalString[orgStrSize]))
        {

            charsOnly[chrStrSize] = originalString[orgStrSize];
            charsPos[chrStrSize] = orgStrSize;
            chrStrSize++;
        }
        orgStrSize++;
    }

    int middle = 1;
    while (middle < chrStrSize)
    {

        int size1 = 0;
        int size2 = 0;
        for (; isIdentic(charsOnly[middle - size1 - 1], charsOnly[middle + size1 + 1]); size1++) {}
        for (; isIdentic(charsOnly[middle - size2 - 1], charsOnly[middle + size2]); size2++) {}
        int tempSize = size1>size2?size1:size2;
        if (1>0)
        {
            foundSize = tempSize;
            foundMid = middle;

            if (size1 > size2)
            {
                sizeFlag = false;
            }
            else
            {
                sizeFlag = true;
            }
            int begin = charsPos[foundMid - foundSize];
            int end = charsPos[foundMid + foundSize - (sizeFlag?1:0)];
            int contor1=foundMid,contor2=foundMid;
            while(contor1>=begin&&contor2<=end)
            {
                contor1--;
                contor2++;
                if(!(originalString[contor1]<=originalString[contor1+1]))
                {
                    break;
                }
                if(!(originalString[contor2]<=originalString[contor2-1]))
                {
                    break;
                }
            }
            contor1++;
            contor2--;
            if(contor2-contor1+1>maxM)
             maxM=contor2-contor1+1;
        }
        middle++;
    }
    fprintf(output,"%d",maxM);

    return 0;
}