Pagini recente » Cod sursa (job #1254534) | Cod sursa (job #2356770) | Cod sursa (job #1961560) | Cod sursa (job #3218244) | Cod sursa (job #2758729)
#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;
}