Cod sursa(job #2541960)

Utilizator robx12lnLinca Robert robx12ln Data 9 februarie 2020 11:36:23
Problema PalM Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include<bits/stdc++.h>
using namespace std;

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

int Dp[505][505], N;
char s[505];
int main(){
    fin >> s + 1;
    N = strlen( s + 1 );
    for( char c  = 'z'; c >= 'a'; c-- ){
        bool ok = false;
        for( int i = 1; i <= N; i++ )
            if( s[i] == c )
                Dp[i][i] = 1, ok = true;
        if( ok == false )
            continue;
        for( int lg = 2; lg <= N; lg++ ){
            for( int i = 1; i + lg - 1 <= N; i++ ){
                int j = i + lg - 1;
                Dp[i][j] = max( Dp[i][j], max( Dp[i + 1][j], Dp[i][j - 1] ) );
                if( s[i] == s[j] && s[i] == c )
                    Dp[i][j] = max( Dp[i][j], Dp[i + 1][j - 1] + 2 );
            }
        }
    }
    fout << Dp[1][N] << "\n";
    return 0;
}