Cod sursa(job #639800)

Utilizator Magnuscont cu nume gresit sau fals Magnus Data 23 noiembrie 2011 22:59:44
Problema PalM Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <cstdio>
#include <cstring>

inline int max(int x,int y){if (x>y) return x;else return y;}

char ch[510];
int d[501][26],e[501][26],f[501][26];

int main()
{
    int n,i,j,k;
    freopen("palm.in","r",stdin);
    freopen("palm.out","w",stdout);
    ch[0]=' ';
    fgets(ch+1,sizeof(ch)-1,stdin);
    n=strlen(ch);
    while (n&&(ch[n]>'z'||ch[n]<'a'))
        --n;
    for (i=1;i<=n;++i)
        for (k=0;k<=ch[i]-'a';++k)
            e[i][k]=1;
    for (i=2;i<=n;++i)
    {
        for (j=1;j<n-i+2;++j)
        {
            for (k=0;k<26;++k)
                f[j][k]=max(e[j][k],e[j+1][k]);
            if (ch[j]==ch[j+i-1])
                for (k=0;k<=ch[j]-'a';++k)
                    f[j][k]=max(f[j][k],d[j+1][ch[j]-'a']+2);
        }
        memcpy(d,e,sizeof(e));
        memcpy(e,f,sizeof(f));
    }
    printf("%d\n",f[1][0]);
    return 0;
}