Cod sursa(job #882316)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 19 februarie 2013 00:10:49
Problema PalM Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <fstream>
#include <cstring>
#define a 97
#define cmax 26
#define nmax 510
using namespace std;

char S[nmax];
int N,Left[cmax][nmax],Right[cmax][nmax];
int DP[cmax+1][nmax][nmax];


void solve() {

    int i,j,k,A,B,L;

    for(i=1;i<=N;i++)
        S[i]-=a;

    for(k=0;k<cmax;k++) {

        for(i=1;i<=N;i++)
            if(S[i]==k)
                Left[k][i]=i;
            else
                Left[k][i]=Left[k][i-1];

        Right[k][N+1]=N+1;
        for(i=N;i>=1;i--)
            if(S[i]==k)
                Right[k][i]=i;
            else
                Right[k][i]=Right[k][i+1];

        }

    for(L=1;L<=N;L++)
        for(i=1;i+L-1<=N;i++)
            for(k=cmax-1;k>=0;k--){

                j=i+L-1;

                A=Right[k][i];
                B=Left[k][j];

                if(A==B)
                    DP[k][i][j]=1;
                else
                if(A<B)
                    DP[k][i][j]=DP[k][A+1][B-1]+2;

                DP[k][i][j]=max(DP[k][i][j],DP[k+1][i][j]);

                }

}
void read() {

    ifstream in("palm.in");
    in.getline(S+1,nmax);
    N=strlen(S+1);
    in.close();

}
void write() {

    ofstream out("palm.out");
    out<<DP[0][1][N]<<'\n';
    out.close();

}
int main() {

    read();
    solve();
    write();

    return 0;

}