Longest Spanning Substrings Problem Code: LNGSUB Codechef Solutions

 

Longest Spanning Substrings Problem Code: LNGSUB
Submit

Read problem statements in MandarinBengaliRussian, and Vietnamese as well.

You are given N strings S1,S2,,SN. Consider a complete undirected graph with N vertices (numbered 1 through N), in which the weight of an edge between vertices u and v is equal to the length of the longest common substring of Su and Sv.

Find the maximum possible weight of a spanning tree of this graph.

Input Format

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N.
  • N lines follow. For each valid i, the i-th of these lines contains a single string Si.

Output Format

For each test case, print a single line containing one integer --- the maximum weight of a spanning tree.

Constraints

  • 1T100
  • 1N100
  • for each valid iSi contains only lowercase English letters
  • the sum of lengths of all strings over all test cases does not exceed 5105

Subtasks

Subtask #1 (10 points): the sum of lengths of all strings over all test cases does not exceed 5102

Subtask #2 (20 points): the sum of lengths of all strings over all test cases does not exceed 5103

Subtask #3 (70 points): original constraints

Sample Input 1 

3
3
bbcab
aa
aab
3
bbc
bcaa
abb
3
aa
acc
abbca

Sample Output 1 

4
4
2

Explanation

Example case 1: The strings S2,S3 have a longest common substring with length 2, and the strings S1,S3 have a longest common substring with length 2. The spanning tree which contains the edges (2,3) and (1,3) has the maximum weight 4.



































































Click On the below Link For Solutions.

—>>Solution Here<<—

Comments