Cod sursa(job #680578)

Utilizator AndrewTheGreatAndrei Alexandrescu AndrewTheGreat Data 15 februarie 2012 19:13:15
Problema PalM Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <iostream>
#include <fstream>
#include <string.h>
#define i64 long long

using namespace std;

const int nmax = 1 << 9;
int bst[nmax][nmax], sigma[nmax], B;
char S[nmax], BC[nmax][nmax];

int main()
{
    ifstream in("palm.in");
    ofstream out("palm.out");

    in.getline(S + 1, nmax);

    i64 M = 1, L = strlen(S + 1), i, j, maxi, w;
    for(i = 1; i < L ; i++)
    {
        for(j = L; j > i; j--)
        {
            maxi = 0, B = - 1;
            for(w = S[j] - 'a'; w >= 0; w--)
                if(sigma[w] != 0 && maxi <= bst[sigma[w]][i])
                {
                    B = sigma[w];
                    maxi = bst[sigma[w]][i];
                }

            if(B == -1) B = 0;

            if(S[i] == S[j])
                bst[L - j + 1][i] = bst[B][i - 1] + 1;
            else
                bst[L - j + 1][i] = max(bst[L - j + 1][i - 1], bst[B][i]);

            if(M < (bst[L - j + 1][i] << 1))
                M = bst[L - j + 1][i] << 1;
            if(M < (bst[B][i] << 1) + 1)
                M = (bst[B][i] << 1) + 1;
            sigma[S[j] - 'a'] = L - j + 1;
        }
        memset(sigma, 0, sizeof(sigma));
    }

    out << M;

    return 0;
}