#include<bits/stdc++.h>
#define N 1020
using namespace std;
struct uva
{
string s;
int l;
};
uva a[N][N];
int main()
{
ios :: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s1, s2;
int i, j, n;
while(getline(cin, s1))
{
n = s1.size();
s2 = s1;
s1 = " " + s1;
reverse(s2.begin(), s2.end());
s2 = " " + s2;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(s1[i] == s2[j])
{
a[i][j].l = a[i-1][j-1].l + 1;
a[i][j].s = a[i-1][j-1].s + s1[i];
}
else
{
if(a[i][j-1].l > a[i-1][j].l)
{
a[i][j].l = a[i][j-1].l;
a[i][j].s = a[i][j-1].s;
}
else if(a[i-1][j].l > a[i][j-1].l)
{
a[i][j].l = a[i-1][j].l;
a[i][j].s = a[i-1][j].s;
}
else
{
a[i][j].l = a[i-1][j].l;
if(a[i-1][j].s < a[i][j-1].s)
a[i][j].s = a[i-1][j].s;
else
a[i][j].s = a[i][j-1].s;
}
}
}
}
string r = a[n][n].s;
int f = r.size()%2, t;
if(f == 1)
t = 0;
else
t = 1;
for(i=0;i<r.size()/2;i++)
{
cout << r[i];
}
for(i=r.size()/2-t;i>=0;i--)
cout << r[i];
cout << "\n";
}
return 0;
}
#define N 1020
using namespace std;
struct uva
{
string s;
int l;
};
uva a[N][N];
int main()
{
ios :: sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s1, s2;
int i, j, n;
while(getline(cin, s1))
{
n = s1.size();
s2 = s1;
s1 = " " + s1;
reverse(s2.begin(), s2.end());
s2 = " " + s2;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(s1[i] == s2[j])
{
a[i][j].l = a[i-1][j-1].l + 1;
a[i][j].s = a[i-1][j-1].s + s1[i];
}
else
{
if(a[i][j-1].l > a[i-1][j].l)
{
a[i][j].l = a[i][j-1].l;
a[i][j].s = a[i][j-1].s;
}
else if(a[i-1][j].l > a[i][j-1].l)
{
a[i][j].l = a[i-1][j].l;
a[i][j].s = a[i-1][j].s;
}
else
{
a[i][j].l = a[i-1][j].l;
if(a[i-1][j].s < a[i][j-1].s)
a[i][j].s = a[i-1][j].s;
else
a[i][j].s = a[i][j-1].s;
}
}
}
}
string r = a[n][n].s;
int f = r.size()%2, t;
if(f == 1)
t = 0;
else
t = 1;
for(i=0;i<r.size()/2;i++)
{
cout << r[i];
}
for(i=r.size()/2-t;i>=0;i--)
cout << r[i];
cout << "\n";
}
return 0;
}
Comments
Post a Comment