You are given a set of strings. You have to find the length of the Optimal Superstring for this set.
Note:
1. A string is said to be the Superstring of a set of strings if all the strings of the set are present in it as a substring.
2. A Superstring is said to be an Optimal Superstring if its length is minimum out of all possible Superstrings for a given set of strings.
3. Each string of the set consists of the uppercase English alphabet.
The first line of the input contains an integer 'N', denoting the size of the set.
The following 'N' lines contain one string denoting the strings, part of the given set.
Output Format:
For each test case, print a single line containing a single integer denoting the length of the optimal Superstring.
The output of each test case will be printed in a separate line.
Note:
You don't have to print anything, it has already been taken care of. Just Implement the given function.
1 <= N <= 14
1 <= |Si| <= 100
Time Limit: 1 sec.
2
AA
AAAA
4
In string “AAAA”, both strings “AA” and “AAAA” are present as substrings.
3
ABC
CDE
EFG
7
In string “ABCDEFG”, all the strings given in the set are present as a substring.
Can we use DP with bitmasking to denote the already added strings?
O((N ^ 2) * 2 ^ N + (N * |L|) ^ 2), where ‘N’ is the size of the set and, ‘|L|’ is the length of string present in the set.
As we are computing values for all states which are (N * 2 ^ N) and for finding the value of each state we need to select which next string will be in the sequence, which takes extra O(N) operation per state. Additionally, we need to precompute the non-overlapping length, of all possible pairs of two strings.
O(N * 2 ^ N + N ^ 2), where ‘N’ is the size of the set and, ‘|L|’ is the length of string present in the set.
As we need to store the answer for each possible state which are of order O(N * 2 ^ N) and also we need to store the non-overlapping length of all possible pairs of two strings.