Cod sursa(job #882326)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 19 februarie 2013 00:16:45
Problema PalM Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 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[nmax][nmax][cmax+1];


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(k=cmax-1;k>=0;k--)
        for(L=1;L<=N;L++)
            for(i=1;i+L-1<=N;i++) {

                A=Right[k][i];
                B=Left[k][i+L-1];

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

                if(DP[i][i+L-1][k]<DP[i][i+L-1][k+1])
                    DP[i][i+L-1][k]=DP[i][i+L-1][k+1];

                }

}
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[1][N][0]<<'\n';
    out.close();

}
int main() {

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

    return 0;

}