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 :



title-img


                            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

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →