Calf Flac


/*
ID: tausiq11
PROG: calfflac
LANG: C++
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main ()
{
    freopen ("calfflac.in", "r", stdin);
    freopen ("calfflac.out", "w", stdout);

    char input [21000] = {0};
    char temp [2005];
    int length;

    while ( gets (temp) ) {
        strcat (input, temp);
        length = strlen (input);
        input [length] = '\n';
        input [++length] = 0;
    }


    length = strlen (input);
    int index = 0;
    char sample [20000];

    for ( int i = 0; i < length; i++ ) {
        if ( isalpha (input [i]) )
            sample [index++] = input [i];
    }

    int count = 0;
    int max = 0;
    int save_right;
    int save_left;

    for ( int i = 1; i < index; i++ ) {
        int right = i + 1;
        int left = i - 1;

        if ( tolower (sample [i]) == tolower (sample [right]) ) {
            count = 2;
            right++;
        }

        else if ( tolower(sample [left]) == tolower (sample [right]) ) {
            count = 3;
            right++;
            left--;
        }

        while ( right < length && left >= 0 &&
        tolower (sample [left]) == tolower (sample [right]) ) {
            right++;
            left--;
            count += 2;
        }

        if ( count > max ) {
            max = count;
            save_left = left + 1;
            save_right = right - 1;
        }
    }

    printf ("%d\n", max);

    int i = 0;
    int temp_i;
    int j = 0;
    int start;
    int end;

    while ( i < length ) {
        if ( input [i] == sample [save_left] ) {
            temp_i = i + 1;
            j = save_left + 1;

            while ( j <= save_right ) {
                while ( !isalpha (input [temp_i]) )
                    temp_i++;

                if ( input [temp_i] != sample [j] )
                    break;

                temp_i++;
                j++;
            }
        }

        if ( j - 1 == save_right ) {
            start = i;
            end = temp_i - 1;
            i = length;
        }

        i++;
    }

    for ( i = start; i <= end; i++ )
        printf ("%c", input [i]);
    printf ("\n");

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s