1551C.md (2865B)
1 Source: https://codeforces.com/problemset/problem/1551/C 2 3 C. Interesting Story 4 5 time limit per test 6 7 4 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Stephen Queen wants to write a story. He is a very unusual writer, he uses only letters 'a', 'b', 'c', 'd' and 'e'! 22 23 To compose a story, Stephen wrote out $n$ words consisting of the first $5$ lowercase letters of the Latin alphabet. He wants to select the maximum number of words to make an interesting story. 24 25 Let a story be a sequence of words that are not necessarily different. A story is called interesting if there exists a letter which occurs among all words of the story more times than all other letters together. 26 27 For example, the story consisting of three words "bac", "aaada", "e" is interesting (the letter 'a' occurs $5$ times, all other letters occur $4$ times in total). But the story consisting of two words "aba", "abcde" is not (no such letter that it occurs more than all other letters in total). 28 29 You are given a sequence of $n$ words consisting of letters 'a', 'b', 'c', 'd' and 'e'. Your task is to choose the maximum number of them to make an interesting story. If there's no way to make a non-empty story, output $0$. 30 31 Input 32 33 The first line contains one integer $t$ ($1 \le t \le 5000$) — the number of test cases. Then $t$ test cases follow. 34 35 The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of the words in the sequence. Then $n$ lines follow, each of them contains a word — a non-empty string consisting of lowercase letters of the Latin alphabet. The words in the sequence may be non-distinct (i. e. duplicates are allowed). Only the letters 'a', 'b', 'c', 'd' and 'e' may occur in the words. 36 37 It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$; the sum of the lengths of all words over all test cases doesn't exceed $4 \cdot 10^5$. 38 39 Output 40 41 For each test case, output the maximum number of words that compose an interesting story. Print 0 if there's no way to make a non-empty interesting story. 42 43 Example 44 45 Input 46 47 ``` 48 6 49 3 50 bac 51 aaada 52 e 53 3 54 aba 55 abcde 56 aba 57 2 58 baba 59 baba 60 4 61 ab 62 ab 63 c 64 bc 65 5 66 cbdca 67 d 68 a 69 d 70 e 71 3 72 b 73 c 74 ca 75 ``` 76 77 Output 78 79 ``` 80 3 81 2 82 0 83 2 84 3 85 2 86 ``` 87 88 Note 89 90 In the first test case of the example, all $3$ words can be used to make an interesting story. The interesting story is "bac aaada e". 91 92 In the second test case of the example, the $1$-st and the $3$-rd words can be used to make an interesting story. The interesting story is "aba aba". Stephen can't use all three words at the same time. 93 94 In the third test case of the example, Stephen can't make a non-empty interesting story. So the answer is $0$. 95 96 In the fourth test case of the example, Stephen can use the $3$-rd and the $4$-th words to make an interesting story. The interesting story is "c bc".