Yet Another KMP Problem


Problem Statement :


This challenge uses the famous KMP algorithm. It isn't really important to understand how KMP works, but you should understand what it calculates.

A KMP algorithm takes a string, S, of length N as input. Let's assume that the characters in S are indexed from 1 to N; for every prefix of S, the algorithm calculates the length of its longest valid border in linear complexity. In other words, for every i (where 1 <= i <= N) it calculates the largest l (where 0 <= l <= i-1) such that for every p (where 1 <= p <= l) there is S[p] = S[i-l+p].

Here is an implementation example of KMP:

kmp[1] = 0;
for (i = 2; i <= N; i = i + 1){
    l = kmp[i - 1];
    while (l > 0 && S[i] != S[l + 1]){
        l = kmp[l];
    }
    if (S[i] == S[l + 1]){
        kmp[i] = l + 1;
    }
    else{
        kmp[i] = 0;
    }
}
Given a sequence x1,x2,...,x26, construct a string, S, that meets the following conditions:

1.The frequency of letter 'a' in S is exactly x1, the frequency of letter 'b' in S is exactly x2, and so on.
Let's assume characters of S are numbered from 1 to N, where Σxi = N. We apply the KMP algorithm to S and get a table, kmp[i], of size N. You must ensure that the sum of kmp[i] for all i is minimal.
If there are multiple strings which fulfill the above conditions, print the lexicographically smallest one.

Input Format

A single line containing 26 space-separated integers describing sequence x.

Constraints

The sum of all xi will be a positive integer <= 10^6.



Solution :



title-img


                            Solution in C :

In C++ :





#include<bits/stdc++.h>
#define mp make_pair
#define PII pair<int,int>
#define fi first
#define se second
using namespace std;

const int NMAX=1000005;

int n,fr[30];
char s[NMAX];

int main()
{
    int i,j,mn,poz,cnt=0,ok;
  //  freopen("date.in","r",stdin);
   // freopen("date.out","w",stdout);
    cin.sync_with_stdio(false);
    mn=1<<30;
    for (i=1;i<=26;i++)
    {
        cin>>fr[i];
        n+=fr[i];
        if (fr[i]<mn && fr[i]>0)
        {
            mn=min(mn,fr[i]);
            poz=i;
        }
        if (fr[i]>0) cnt++;
    }

    if (cnt==1)
    {
        for (i=1;i<=n;i++) s[i]=poz+'a'-1;
        cout<<(s+1)<<"\n";
        return 0;
    }
    for (i=1;i<=n;i++) s[i]='>';
    s[1]=poz+'a'-1;
    fr[poz]--;

    ok=0;
    for (j=1;j<=26 && !ok;j++)
        if (fr[j]>0)
        {
            fr[j]--;
            s[2]=j+'a'-1;
            ok=1;
        }
    i=3;
    if (s[1]==s[2])//a 3-a neaparat diferita
    {
        i=4;
        while (fr[poz])
        {
            s[i]=poz+'a'-1;
            fr[poz]--;
            i+=2;
        }
        cnt=0;
        for (i=1;i<=26;i++) cnt+=fr[i];
        i=1;j=1;
        while (cnt)
            {
                while (s[i]!='>') i++;
                while (fr[j]==0) j++;
                s[i]=j+'a'-1;fr[j]--;
                cnt--;
            }
    }
    else
    {
        i=3;
        for (j=1;j<=26;j++)
            while (fr[j])
            {
                s[i]=j+'a'-1;
                fr[j]--;
                i++;
            }
    }
    cout<<(s+1)<<"\n";
    return 0;
}







In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


    public static int[] readIntArray3(Scanner in, int size) {
        int[] arr = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = in.nextInt();
        }
        return arr;
    }



    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        int cases = 1;//in.nextInt();
        for (int testcase = 0; testcase < cases; testcase++) {
            int[] arr = readIntArray3(in, 26);
            kmpproblem(arr);
        }
    }

    public static void kmpproblem(int[] arr) {
        int least = Integer.MAX_VALUE;
        int leastIndex = -1;
        int smallestIndex = -1;
        for (int i = 0; i < 26; i++) {
            if (arr[i] < least && arr[i] > 0) {
                least = arr[i];
                leastIndex = i;
            }
            if (smallestIndex == -1 && arr[i] > 0) {
                smallestIndex = i;
            }
        }
//        System.out.println(leastIndex + ": " + least);
//        System.out.println(smallestIndex);
        if (leastIndex == -1)  {
            System.out.println("");
            return;
        }
        if (smallestIndex != leastIndex) {
            System.out.print((char)(leastIndex+'a'));
            arr[leastIndex]--;
            for (int i = 0; i < 26; i++) {
                for (int j = 0; j < arr[i]; j++) {
                    System.out.print((char)(i+'a'));
                }
            }
            return;
        }
        System.out.print((char)(leastIndex+'a'));
        arr[leastIndex]--;
        for (int i = leastIndex+1; i < 26; i++) {
            for (int j = 0; j < arr[i]; j++) {
                if (arr[leastIndex] > 0) {
                    System.out.print((char)(leastIndex+'a'));
                    arr[leastIndex]--;
                }
                System.out.print((char)(i+'a'));
            }
        }
        while (arr[leastIndex] > 0) {
            System.out.print((char)(leastIndex+'a'));
            arr[leastIndex]--;
        }

    }
}










#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int letter = 0, min=26, first = -1;
    int data[27];
    data[26]=1000001;
    for(int i = 0; i < 26; i++){
        scanf("%d",data+i);
        if(data[i]) {
            if (first<0) first = i;
            letter++;
            if(data[i]<data[min]) min = i;
        }
    }
    if(letter == 1) {
        for(int i = 0; i < data[min]; i++) {
            putchar('a'+min);
        }
        return 0;
    }
    if (min==first) {
        putchar('a'+min);
        int index_m = 1;
        for (int l = first + 1; l < 26; l++) {
            for (int i = 0; i<data[l]; i++) {
                if(index_m++ < data[min]) putchar('a'+min);
                putchar('a'+l);
            }
        }
    } else {
        putchar('a'+min);
        data[min]--;
        for (int l = first; l < 26; l++) {
            for (int i = 0; i<data[l]; i++) {
                putchar('a'+l);
            }
        }
    }
    
    return 0;
}









In Python3 :





from string import ascii_lowercase as A
import sys
x = list(map(int, input().split()))
used = [i for i in range(26) if x[i]]
if len(used) == 0:
    sys.exit()
if len(used) == 1:
    print(A[used[0]] * x[used[0]])
else:
    f = min(used, key=lambda a: (x[a], a))
    used = [a for a in used if a != f]
    if x[f] == 1:
        print(A[f] + ''.join(A[c]*x[c] for c in used))
    elif f > used[0]:
        x[f] -= 1
        print(A[f] + ''.join(A[c]*x[c] for c in range(26)))
    elif 2*(x[f]-2) <= sum(x)-2:
        res = 2*A[f]
        b = ''.join(A[c]*x[c] for c in used)
        x[f] -= 2
        i = 0
        while x[f]:
            res += b[i] + A[f]
            i += 1
            x[f] -= 1
        res += b[i:]
        print(res)
    else:
        x[f] -= 1
        x[used[0]] -= 1
        print(A[f] + A[used[0]] + A[f]*x[f] + ''.join(A[c]*x[c] for c in used))
                        








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 →