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

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →