Pagini recente » Borderou de evaluare (job #1601053) | Rezultatele filtrării | Cod sursa (job #2415490) | Cod sursa (job #2537336)
#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;
}
for (int i = n; i >= 1; --i) {
for (int j = i; j <= n; ++j) {
if (i == j)
dp[i][j] = 1;
else {
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
for (int c = str[i] - 'a' + 1; c <= 26; ++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);
}
}
}
}
cout << dp[1][n];
return 0;
}