**Play with words**

### Problem Statement :

Shaka and his brother have created a boring game which is played like this: They take a word composed of lowercase English letters and try to get the maximum possible score by building exactly 2 palindromic subsequences. The score obtained is the product of the length of these 2 subsequences. Let's say A and B are two subsequences from the initial string. If Ai & Aj are the smallest and the largest positions (from the initial word) respectively in A; and Bi & Bj are the smallest and the largest positions (from the initial word) respectively in B, then the following statements hold true: Ai <= Aj, Bi <= Bj, & Aj < Bi. i.e., the positions of the subsequences should not cross over each other. Hence the score obtained is the product of lengths of subsequences A & B. Such subsequences can be numerous for a larger initial word, and hence it becomes harder to find out the maximum possible score. Can you help Shaka and his brother find this out? Input Format Input contains a word S composed of lowercase English letters in a single line. Constraints 1 < |S| <= 3000 each character will be a lower case english alphabet. Output Format Output the maximum score the boys can get from S.

### Solution :

` ````
Solution in C :
In C++ :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
char a[3333];
int n;
int f[3033][3033];
int get(int l, int r) {
if (l > r) return 0;
if (l == r) {
f[l][r] = 1;
}
if (f[l][r] != 0) {
return f[l][r];
}
f[l][r] = max(get(l + 1, r), get(l, r - 1));
if (a[l] == a[r]) {
f[l][r] = max(f[l][r], get(l + 1, r - 1) + 2);
}
return f[l][r];
}
int main() {
string s;
cin >> s;
n = s.size();
for (int i = 0; i < n; i++) {
a[i + 1] = s[i];
}
int ans = 0;
for (int i = 1; i < n; i++) {
ans = max(ans, get(1, i) * get(i + 1, n));
}
cout << ans << endl;
return 0;
}
In Java :
/**
* Created by zhengf1 on 12/18/2014.
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Dec101_C {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int n = str.length();
int[][] L = new int[n][n];
for (int i = 0; i < n; i++)
L[i][i] = 1;
int i = 0;
int j = 0;
int cl = 0;
for (cl=2; cl<=n; cl++)
{
for (i=0; i<n-cl+1; i++)
{
j = i+cl-1;
if (str.charAt(i) == str.charAt(j) && cl == 2)
L[i][j] = 2;
else if (str.charAt(i) == str.charAt(j))
L[i][j] = L[i+1][j-1] + 2;
else
L[i][j] = Math.max(L[i][j - 1], L[i + 1][j]);
}
}
int res = 0;
for(i = 1; i < n; i++){
int v1 = L[0][i - 1];
int v2 = L[i][n - 1];
res = Math.max(res, v1 * v2);
}
System.out.println(res);
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int L[3100][3100],cntt[30][3100];
int max(int a,int b)
{
if(a<b)
{
return b;
}
return a;
}
int main() {
char str[4000],c;
int i,j,t,l,cl;
scanf("%s",str);
l=strlen(str);
for(i=0;i<l;i++)
{
L[i][i]=1;
}
for (cl=2; cl<=l; cl++)
{
for (i=0; i<l-cl+1; i++)
{
j = i+cl-1;
if (str[i] == str[j] && cl == 2)
L[i][j] = 2;
else if (str[i] == str[j])
L[i][j] = L[i+1][j-1] + 2;
else
L[i][j] = max(L[i][j-1], L[i+1][j]);
}
}
int mx=0;
for(i=0;i<l;i++)
{
if((L[0][i]*L[i+1][l-1])>mx)
{
mx=(L[0][i]*L[i+1][l-1]);
}
}
printf("%d\n",mx);
return 0;
}
In Python3 :
s = input()
p = [[0] * len(s) for _ in range(len(s))]
for i in range(len(s)): p[i][i] = 1
for k in range(2, len(s) + 1):
for i in range(len(s) - k + 1):
j = i + k - 1
if s[i] == s[j]:
p[i][j] = 2 + (0 if k == 2 else p[i + 1][j - 1])
else:
p[i][j] = max(p[i][j - 1], p[i + 1][j])
print(max(p[0][i] * p[i + 1][-1] for i in range(len(s) - 1)))
```

## View More Similar Problems

## Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

View Solution →## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

View Solution →## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →## The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

View Solution →## Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →