Pagini recente » Cod sursa (job #1591927) | Cod sursa (job #2489584) | Cod sursa (job #606246) | Cod sursa (job #2893320) | Cod sursa (job #882327)
Cod sursa(job #882327)
#include <fstream>
#include <cstring>
#define a 97
#define cmax 26
#define nmax 510
using namespace std;
char S[nmax];
int N,Left[cmax][nmax],Right[cmax][nmax];
int DP[nmax][nmax][cmax+1];
void solve() {
int i,j,k,A,B,L;
for(i=1;i<=N;i++)
S[i]-=a;
for(k=0;k<cmax;k++) {
for(i=1;i<=N;i++)
if(S[i]==k)
Left[k][i]=i;
else
Left[k][i]=Left[k][i-1];
Right[k][N+1]=N+1;
for(i=N;i>=1;i--)
if(S[i]==k)
Right[k][i]=i;
else
Right[k][i]=Right[k][i+1];
}
for(L=1;L<=N;L++)
for(i=1;i+L-1<=N;i++)
for(k=cmax-1;k>=0;k--) {
A=Right[k][i];
B=Left[k][i+L-1];
if(A==B)
DP[i][i+L-1][k]=1;
else
if(A<B)
DP[i][i+L-1][k]=DP[A+1][B-1][k]+2;
if(DP[i][i+L-1][k]<DP[i][i+L-1][k+1])
DP[i][i+L-1][k]=DP[i][i+L-1][k+1];
}
}
void read() {
ifstream in("palm.in");
in.getline(S+1,nmax);
N=strlen(S+1);
in.close();
}
void write() {
ofstream out("palm.out");
out<<DP[1][N][0]<<'\n';
out.close();
}
int main() {
read();
solve();
write();
return 0;
}