Cod sursa(job #2771006)

Utilizator tomaionutIDorando tomaionut Data 24 august 2021 17:32:28
Problema PalM Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("palm.in");
ofstream fout("palm.out");
char s[505],ch;
int dp[505][505];
int n;
int main()
{
    int i,j,d,sol=0;
    fin >> (s + 1);
    n = strlen(s + 1);
    for (ch = 'z'; ch >= 'a'; ch--)
    {
        for (d = 2; d <= n; d++)
        {
            for (i = 1; i <= n - d + 1; i++)
            {
                j = i + d - 1;
                if (s[i] == ch)
                {
                    dp[i][i] = 1;
                    if (s[i] == s[j]) dp[i][j] = max(dp[i][j], 2 + dp[i + 1][j - 1]);
                }
                dp[i][j] = max({ dp[i][j], dp[i + 1][j],dp[i][j - 1] });
                sol = max(sol, dp[i][j]);
            }
        }
    }
    fout << sol;

    return 0;
}