Pagini recente » Cod sursa (job #1363576) | Cod sursa (job #772181) | Cod sursa (job #808440) | Cod sursa (job #1234448) | Cod sursa (job #1549664)
#include <fstream>
#include <string>
using namespace std;
ifstream fin("elimin2.in");
ofstream fout("elimin2.out");
typedef short int i16;
const i16 nmax= 2002;
const i16 cmax= 9;
bool u[nmax+2];
i16 n;
i16 a[nmax+2], x[cmax+2][nmax+2], y[cmax+1][nmax+2], d[nmax+2][nmax+2];
int main( ) {
string s;
fin>>s;
n= s.size();
for ( i16 i= 1; i<=n; ++i ) {
a[i]= s[i-1]-'0';
for ( i16 j= 0; j<=9; ++j ) {
y[j][i]= y[j][i-1];
}
y[a[i]][i]= i;
d[i][i]= 1;
}
for ( i16 i= n; i>=1; --i ) {
for ( i16 j= 0; j<=9; ++j ) {
x[j][i]= x[j][i+1];
}
x[a[i]][i]= i;
}
for ( i16 i= 1; i<=n-1; ++i ) {
for ( i16 j= 1; j<=n-i; ++j ) {
i16 aux1= y[a[j]][j+i]-1, aux2= x[a[j+i]][j]+1;
d[j][j+i]= max(d[j+1][j+i], d[j][j+i-1]);
if ( aux1!=0 && j<=aux1 ) {
d[j][j+i]= max(d[j][j+i], (i16)(d[j+1][aux1]+2));
}
if ( aux2!=0 && aux2<=j+i ) {
d[j][j+i]= max(d[j][j+i], (i16)(d[aux2][j+i-1]+2));
}
}
}
for ( i16 i= 1, j= n, start= 1; i<=j; start= 0 ) {
i16 len= 0, pos= 0;
for ( i16 cif= 9; cif>=start; --cif ) {
if ( x[cif][i]<=j && y[cif][j]>=i ) {
i16 auxlen= 0;
if ( x[cif][i]==y[cif][j] ) {
auxlen= 1;
} else {
auxlen= d[x[cif][i]+1][y[cif][j]-1]+2;
}
if ( auxlen>len ) {
len= auxlen;
pos= cif;
}
}
}
u[x[pos][i]]= u[y[pos][j]]= 1;
i= x[pos][i]+1, j= y[pos][j]-1;
}
for ( i16 i= 1; i<=n; ++i ) {
if ( u[i]==1 ) {
fout<<a[i];
}
}
fout<<"\n";
return 0;
}