E-Shortest name

Source: Internet
Author: User

Description

In a strange village, many people have long names, such as AAAAA, BBB and ababab. It is obviously inconvenient to call the full name because the name is so long. Therefore, it is generally called the prefix of a name between villagers. For example, you can call 'aaaaa' only, because the first three letters without a second name are 'aaa '. However, you cannot call it 'A' because two people have names starting with 'A. People in the village are very smart. They always use the shortest name. Input to ensure that no one in the village will have a prefix with the name of another (as an inference, no two will have the same name ). If a person in the village wants to name everyone (including himself), how many letters will he say?

Input

T (t <= 10 ). The first behavior of each group of data is an integer N (1 <=n <= 1000), that is, the number of people in the village. The name of each person in the following n rows (consisting of only lowercase letters ). Input to ensure that the length of the name of all persons in a village cannot exceed 1,000,000.

Output

For each group of data, the total number of letters in the name of the person is output.

Sample Input

13aaaaabbbabababab 

Sample output

5 



1 # include "iostream" 2 # include "algorithm" 3 # include "vector" 4 # include "cstdio" 5 # include "cstring" 6 using namespace STD; 7 int main () 8 {9 int I, j, t, n, k, ans; 10 int CNT [2, 1005]; 11 scanf ("% d", & T ); 12 while (t --) 13 {14 scanf ("% d", & N); 15 vector <string> VEC; 16 string STR; 17 memset (CNT, 0, sizeof (CNT); 18 for (I = 0; I <n; I ++) 19 {20 CIN> STR; 21 Vec. push_back (STR); 22} 23 sort (VEC. begin (), VEC. end (); 24 for (I = 0; I <n-1; I ++) // no one's name is the prefix 25 {26 for (j = 0; j <VEC [I]. size () & J <VEC [I + 1]. size () & VEC [I] [J] = VEC [I + 1] [J]; j ++ ); 27 if (J + 1> CNT [I]) 28 CNT [I] = J + 1; 29 If (J + 1> CNT [I + 1]) // The first J letters are the same 30 CNT [I + 1] = J + 1; 31} 32 for (ANS = I = 0; I <n; I ++) 33 ans + = CNT [I]; 34 printf ("% d \ n", ANS); 35} 36 return 0; 37}

E-Shortest name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.