Pagini recente » Cod sursa (job #1703009) | Cod sursa (job #377611) | Cod sursa (job #1086959) | Cod sursa (job #971695) | Cod sursa (job #53827)
Cod sursa(job #53827)
#include <stdio.h>
#include <string.h>
#define maxim(a, b) ((a > b) ? a : b)
#define NMax 2005
short int N, D[NMax][NMax], L[10][NMax], R[10][NMax], Res[NMax];
char S[NMax];
int main(void)
{
int i, j, d, c, max, x, y, sx, sy;
freopen("elimin2.in", "r", stdin);
freopen("elimin2.out", "w", stdout);
scanf("%s", &S);
N = strlen(S);
if (N > 2000)
for (;;);
for (c = 0; c < 10; c++)
{
L[c][N] = 30000;
for (i = N-1; i >= 0; i--)
if (S[i]-'0' == c) L[c][i] = i;
else L[c][i] = L[c][i+1];
}
for (c = 0; c < 10; c++)
{
R[c][0] = -30000; if (S[0]-'0' == c) R[c][0] = 0;
for (i = 1; i < N; i++)
if (S[i] - '0' == c) R[c][i] = i;
else R[c][i] = R[c][i-1];
}
for (i = 0; i < N; i++) D[i][i] = 1;
for (d = 2; d <= N; d++)
for (i = 0; i <= N-d; i++)
{
j = i+d-1;
D[i][j] = maxim(D[i+1][j-1], maxim(D[i][j-1], D[i+1][j]));
if (L[S[j]-'0'][i] < j)
D[i][j] = maxim(D[i][j], D[L[S[j]-'0'][i]+1][j-1] + 2);
if (R[S[i]-'0'][j] > i)
D[i][j] = maxim(D[i][j], D[i+1][R[S[i]-'0'][j]-1] + 2);
}
max = d = D[0][N-1]; x = -1; y = N;
for (i = 1; i <= (d+1)/2; i++)
for (c = 9; c >= 0; c--)
{
sx = L[c][x+1]; sy = R[c][y-1];
if (sx <= sy && max == D[sx+1][sy-1] + 1 + (sx < sy))
{
max -= 1 + (sx < sy),
x = sx, y = sy,
Res[i] = Res[d+1-i] = c;
break;
}
}
// eliminam 0-urile terminale
x = 1; y = d;
while (Res[x] == 0) x++, y--;
for (i = x; i <= y; i++)
printf("%d", Res[i]);
printf("\n");
return 0;
}