Minimize Trie Nodes

1

2 votes
Advanced Data Structures, Trie, C++, Data Structures
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.

Problem statement

You are given N strings. Each string is given in the form of an array cnt of size 26 where the ith element in array denotes the count of the ith character of lowercase English alphabets in the string.

You have to select exactly 4 distinct strings.

These strings will be inserted into a Trie in an optimal way (which might include shuffling of the characters), ensuring that the number of nodes in the Trie is minimized by the checker.

Your goal is to minimize the value of function Z,

  • Z = Number of nodes in the Trie.

Notes

  • Trie will always have a root node. Include root node in the count of nodes present in Trie.
  • Insertion proceeds by walking the Trie according to the string to be inserted, then appending new nodes for the suffix of the string that is not contained in the Trie.

Input format

  • The first line contains an integer N.
  • The next N lines contain 26 space-separated integers denoting the count of each lowercase English alphabet in the string.

Output format

Print 4 space separated integers (1-based), denoting the indices of the strings selected.

Constraints

N=3000cnt[i][j]105No string is empty

Verdit and Scoring

  • is the score for each test case.
  • If the indices are invalid, then you will receive a Wrong Answer verdict.

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Approach

  • Checker will shuffle the characters of first 4 strings such that strings are:
    • baac
    • bbcd
    • bbb
    • baab
  • baac, bbcd, bbb, baab: Minimum number of nodes in Trie will be 10 (including root node).

​​​​​​​

  • It can be proved that you cannot make a Trie with less than 10 nodes.
  • Thus, value of Z = 10
Editor Image

?