Pagini recente » Cod sursa (job #1352049) | Cod sursa (job #2537343)
#include <bits/stdc++.h>
using namespace std;
const int DIM = 505;
const int SGM = 26;
int dp[DIM][DIM], nxt[DIM][SGM], prv[DIM][SGM];
char str[DIM];
int main(void) {
freopen("palm.in", "r", stdin);
freopen("palm.out", "w", stdout);
int n;
cin >> (str + 1);
n = strlen(str + 1);
for (int i = 1; i <= n; ++i) {
for (int c = 0; c <= 25; ++c)
prv[i][c] = prv[i - 1][c];
prv[i][str[i] - 'a'] = i;
}
for (int c = 0; c <= 25; ++c)
nxt[n + 1][c] = n + 1;
for (int i = n; i >= 1; --i) {
for (int c = 0; c <= 25; ++c)
nxt[i][c] = nxt[i + 1][c];
nxt[i][str[i] - 'a'] = i;
}
int ans = 0;
for (int i = n; i >= 1; --i) {
for (int j = i; j <= n; ++j) {
if (i == j)
dp[i][j] = 1;
else if (i != j && str[i] == str[j]) {
dp[i][j] = 2;
for (int c = str[i] - 'a'; c <= 25; ++c) {
int p = nxt[i + 1][c], q = prv[j - 1][c];
if (p <= q && p <= j && i <= q)
dp[i][j] = max(dp[i][j], dp[p][q] + 2);
}
}
ans = max(ans, dp[i][j]);
}
}
cout << ans;
return 0;
}