Cod sursa(job #1929726)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 17 martie 2017 23:28:52
Problema PalM Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>

const int kMaxDim = 505;

char text[kMaxDim];
int dp[kMaxDim][kMaxDim];

int main() {
	std::ifstream inputFile("palm.in");
	std::ofstream outputFile("palm.out");

	inputFile >> (text + 1);
	int n = strlen(text + 1);

	for (char currLet = 'z'; currLet >= 'a'; --currLet) {

		for (int i = 1; i <= n; ++i)
			if (text[i] == currLet)
				dp[i][i] = 1;

		for (int len = 2; len <= n; ++len) {

			for (int i = 1, j = i + len - 1; j <= n; ++i, ++j) {
				if (text[i] == text[j] && text[i] == currLet)
					dp[i][j] = dp[i + 1][j - 1] + 2;

				dp[i][j] = std::max(dp[i][j], dp[i + 1][j]);
				dp[i][j] = std::max(dp[i][j], dp[i][j - 1]);
			}

		}

	}

	outputFile << dp[1][n] << '\n';

	inputFile.close();
	outputFile.close();

	return 0;
}

//Trust me, I'm the Doctor!