Rajiv M.

asked • 07/23/20

java programming

Some people remain old fashioned and John is one of them. He doesn't like the new smart phones with full keypads and still uses the old keypads which require you to tap a key multiple times to type a single letter. For example, if the keyboard has two keys, one with the letters "adef" and the other one with the letters "zyx", then typing 'a' requires one keystroke, typing 'f' requires four keystrokes, typing 'y' requires two keystrokes, and so on.

He recently moved to a new country where the language is such that his keypad is not the most efficient. In every language some characters occur more often than others. He wants to create a specific keyboard for this language that uses N different letters. He has a large body of text in this language, and has already analyzed it to find the frequencies of all N letters of its alphabet.

You are given an array 'frequencies' with N elements. Each element of frequencies is the number of times one of the letters in the new language appears in the text John has. Each element of frequencies will be strictly positive. (I.e., each of the N letters occurs at least once.)

You are also given an array keySize. The number of elements of keySize is the number of keys on the keyboard. Each element of keySize gives the maximal number of letters that maybe put on one of the keys.

Find an assignment of letters to keys that minimizes the number of keystrokes needed to type the entire text. Output that minimum number of keystrokes. If there is not enough room on the keys and some letters of the alphabet won't fit, Output -1 instead.

Input Format

  1. The first line will contain a number 'N' that specifies the size of 'frequencies' array
  2. The second line will contain N numbers that form the frequencies array
  3. The third line contains a number 'K' that specifies the size of the 'keySize' array
  4. The fourth line contains K numbers that form the keySize array

Output Format

  1. Output a single integer that is answer to the problem.

Constraints

  1. frequencies will contain between 1 and 50 elements, inclusive.
  2. Each element of frequencies will be between 1 and 1,000, inclusive.
  3. keySizes will contain between 1 and 50 elements, inclusive.
  4. Each element of keySizes will be between 1 and 50, inclusive.

SAMPLE INPUT

 

4
7 3 4 1
2
2 2

SAMPLE OUTPUT

 

19

Explanation

The foreign language has four letters. Let us call them W, X, Y and Z. John's text contains seven Ws, three Xs, four Ys, and one Z. The keyboard has two keys, each of them may contain at most two letters. One optimal solution is to use the keys "WZ" and "YX". We can then type each W and each Y using a single keystroke, and we need two keystrokes for each X and each Z. Therefore, the total number of keystrokes when typing the entire text will be 71 + 32 + 41 + 12 = 19.


Patrick B.

I have uploaded the source code for you, and it is under the TOOLKIT/RESOURCES menu. It is a text file named keyboard.txt; the title is Keyboard Mapping and is found under this link. There are THREE(3) source code files bundled; Alpha.java, KeyMap.java, and Virtual Keyboard.java; Split this text file into those three files, compiler them, and then run via the command java VirtualKeyboard; I changed the sort from Insertion to Selection, which is just as efficient as there are only 26 frequency records
Report

07/23/20

1 Expert Answer

By:

Patrick B. answered • 07/23/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.