Cod sursa(job #2258220)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 11 octombrie 2018 00:13:14
Problema PalM Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <fstream>
#define VAL 505

using namespace std;

ifstream fin("palm.in");
ofstream fout("palm.out");

int N, i, j, CAR, LEN;
int dp[VAL][VAL];
string S;

int main()
{
    fin >> S;
    N=S.size();
    for (CAR='z'; CAR>='a'; CAR--)
    {
        for (j=0; j<N; j++)
            if (S[j]==CAR)
                dp[j][j]=1;
        for (LEN=1; LEN<N; LEN++)
        {
            for (i=0; i<N-LEN; i++)
            {
                j=i+LEN;
                if (S[i]==S[j] && S[i]==CAR)
                    dp[i][j]=max(dp[i][j], dp[i+1][j-1]+2);
                dp[i][j]=max(dp[i][j], dp[i+1][j]);
                dp[i][j]=max(dp[i][j], dp[i][j-1]);
            }
        }
    }
    fout << dp[0][N-1] << '\n';
    fin.close();
    fout.close();
    return 0;
}