Interesting Story
Contest: Codeforces Round 734 (Div. 3)
Difficulty rating: 1500
Tags: greedy, sortings, strings

Stephen Queen wants to write a story. He is a very unusual writer, he uses only letters 'a', 'b', 'c', 'd' and 'e'!

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.

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.

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).

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$$$.

Input format:
The first line contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. Then $$$t$$$ test cases follow.

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.

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$$$.

Output format:
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.

Constraints:
Time limit: 4.0 s
Memory limit: 256.0 MB

Note:
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".

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.

In the third test case of the example, Stephen can't make a non-empty interesting story. So the answer is $$$0$$$.

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".

Example input:
6
3
bac
aaada
e
3
aba
abcde
aba
2
baba
baba
4
ab
ab
c
bc
5
cbdca
d
a
d
e
3
b
c
ca

Example output:
3
2
0
2
3
2

Editorial:
Let $$$f(s, c)$$$ be the number of the occurrences of the letter $$$c$$$ in the word $$$s$$$ minus the number of the occurrences of all other letters in total. Since for each two words $$$s_1$$$ and $$$s_2$$$ the number of the occurrences of a letter in the word $$$s_1 + s_2$$$ is the sum of the numbers of its occurrences in $$$s_1$$$ and $$$s_2$$$, the equality $$$f(s_1 + s_2, c) = f(s_1, c) + f(s_2, c)$$$ is true ($$$s_1 + s_2$$$ means the concatenation of $$$s_1$$$ and $$$s_2$$$).
Consider a sequence of words $$$s_1, s_2, \ldots, s_n$$$. A story consisting of words $$$s_{i_1}, s_{i_2}, \ldots, s_{i_m}$$$ is interesting if and only if there's a letter $$$c$$$ such that $$$f(s_{i_1} + s_{i_2} + \ldots + s_{i_m}, c) > 0$$$ — it exactly means that there's a letter which occurs more times than all other in total. So we are interested in searching for a letter $$$c$$$ such that exists a positive integer $$$m$$$ — a maximal number of words $$$s_{i_1}, s_{i_2}, \ldots, s_{i_m}$$$ such that $$$\sum\limits_{j = 1}^{m} f(s_{i_j}, c) = f(s_{i_1} + s_{i_2} + \ldots + s_{i_m}, c) > 0$$$.
Suppose we have a set of words that form an interesting story and where $$$c$$$ is the letter having more occurrences than all other letters in total. Suppose we can add to it one of few words. We had better add a word $$$s$$$ such that $$$f(s, c)$$$ is maximal to be able to add more words in the future.
So the problem has the following solution: for each letter $$$c$$$ of the Latin alphabet and for each word $$$s_i$$$ let's calculate $$$f(s, c)$$$. Then let's iterate over all letters $$$c$$$, take a sequence $$$f(s_1, c), f(s_2, c), \ldots, f(s_n, c)$$$ and sort it in descending order. Let's initialize an interesting story by a set of a single word corresponding to the first element of the sequence. If there's no word $$$s$$$ such that $$$f(s, c) \le 0$$$, then there's no non-empty interesting story containing some words of the given set. Otherwise, let's take the next elements of the sequence sequentially until the sum of $$$f(s, c)$$$ over all taken words $$$s$$$ is greater than zero. Let's select a letter such that the corresponding taken set is maximal over all letters. Finally, we should print the set's size.
The solution consists of two phases: the calculation of all $$$f(s, c)$$$ (works in $$$O(L \times \sum\limits_{i = 1}^{n} |s_i|)$$$ where $$$L$$$ is the alphabet's size, $$$|s|$$$ is the lengths of a string $$$s$$$) and building a maximal interesting story for each letter $$$c$$$ (sorting and a greedy algorithm — $$$O(L \times n \times \log n)$$$).