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

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

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 →