Cod sursa(job #2758729)

Utilizator puica2018Puica Andrei puica2018 Data 12 iunie 2021 11:51:06
Problema Elimin 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;

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

int n;
char s[2005];

bool found;

short toLeft[2005][10],toRight[2005][10],dp[2005][2005];

void solve(int i,int j,short x)
{
    if(x<=0)
        return;
    for(int cif=9; cif>=0; cif--)
    {
        if(dp[toRight[i][cif]][toLeft[j][cif]]==x)
        {
            fout<<cif;
            solve(toRight[i][cif]+1,toLeft[j][cif]-1,x-2);
            if(x>1)
                fout<<cif;
            found=1;
            break;
        }
    }
}

int main()
{
    fin>>(s+1);
    n=strlen(s+1);
    int i,j;
    for(i=1; i<=n; i++)
    {
        for(int cif=0; cif<=9; cif++)
            toLeft[i][cif]=toLeft[i-1][cif];
        toLeft[i][s[i]-'0']=i;
    }
    for(i=n; i>=1; i--)
    {
        for(int cif=0; cif<=9; cif++)
            toRight[i][cif]=toRight[i+1][cif];
        toRight[i][s[i]-'0']=i;
    }
    for(i=1; i<=n; i++)
        dp[i][i]=1;
    for(int l=2; l<=n; l++)
    {
        for(i=1; i<=n-l+1; i++)
        {
            j=i+l-1;
            if(s[i]==s[j])
                dp[i][j]=dp[i+1][j-1]+2;
            else
                dp[i][j]=max(dp[i+1][j],dp[i][j-1]);
        }
    }
    short maxim=0;
    for(int cif=1; cif<=9; cif++)
        maxim=max(maxim,dp[toRight[1][cif]][toLeft[n][cif]]);
    solve(1,n,maxim);
    if(!found)
        fout<<"0";
    fout<<"\n";
    return 0;
}