Interesting Story (Codeforces 1551C)
Tags: Greedy, 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: 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: 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.

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

Sample Inputs:
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

Sample Outputs:
3
2
0
2
3
2

Solution (Python):
import sys

def get_string():
    return sys.stdin.readline().strip()

def get_int():
    return int(sys.stdin.readline().strip())

def solve(n, arr):
    total = 0
    store = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0}
    a = []
    b = []
    c = []
    d = []
    e = []
    for ele in arr:
        total = len(ele)
        store = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0}
        for char in ele:
            store[char] += 1
        a.append(2 * store['a'] - total)
        b.append(2 * store['b'] - total)
        c.append(2 * store['c'] - total)
        d.append(2 * store['d'] - total)
        e.append(2 * store['e'] - total)
    a.sort()
    b.sort()
    c.sort()
    d.sort()
    e.sort()
    best = 0
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += a[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += b[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += c[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += d[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += e[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    sys.stdout.write(str(best) + '\n')
    return
T = get_int()
while T:
    n = get_int()
    tempN = n
    arr = []
    while tempN:
        s = get_string()
        arr.append(s)
        tempN -= 1
    solve(n, arr)
    T -= 1

Solution (Python):
import sys

def get_string():
    return sys.stdin.readline().strip()

def solve(n, arr):
    total = 0
    store = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0}
    a = []
    b = []
    c = []
    d = []
    e = []
    for ele in arr:
        total = len(ele)
        store = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0}
        for char in ele:
            store[char] += 1
        a.append(2 * store['a'] - total)
        b.append(2 * store['b'] - total)
        c.append(2 * store['c'] - total)
        d.append(2 * store['d'] - total)
        e.append(2 * store['e'] - total)
    a.sort()
    b.sort()
    c.sort()
    d.sort()
    e.sort()
    best = 0
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += a[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += b[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += c[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += d[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    current = 0
    temp = 0
    for i in range(n - 1, -1, -1):
        current += e[i]
        if current > 0:
            temp += 1
        else:
            break
    best = max(best, temp)
    sys.stdout.write(str(best) + '\n')
    return
T = int(get_string())
while T:
    n = int(get_string())
    tempN = n
    arr = []
    while tempN:
        s = get_string()
        arr.append(s)
        tempN -= 1
    solve(n, arr)
    T -= 1

Solution (Python):
def main():
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    inf = 1e+17
    mod = 10 ** 9 + 7

    def factorial(n):
        f = 1
        for i in range(1, n + 1):
            f = f * i % mod
        return f

    def ncr(n, r):
        num = den = 1
        for i in range(r):
            num = num * (n - i) % mod
            den = den * (i + 1) % mod
        return num * pow(den, mod - 2, mod) % mod

    def solve(n, words):
        answer = 0
        a = []
        for word in words:
            score = 0
            for letter in word:
                if letter == 'a':
                    score += 1
                else:
                    score -= 1
            a.append(score)
        a.sort(reverse=True)
        still = 0
        cnt = 0
        for j in range(len(a)):
            if still + a[j] > 0:
                still += a[j]
                cnt += 1
            else:
                break
        answer = max(answer, cnt)
        b = []
        for word in words:
            score = 0
            for letter in word:
                if letter == 'b':
                    score += 1
                else:
                    score -= 1
            b.append(score)
        b.sort(reverse=True)
        still = 0
        cnt = 0
        for j in range(len(b)):
            if still + b[j] > 0:
                still += b[j]
                cnt += 1
            else:
                break
        answer = max(answer, cnt)
        c = []
        for word in words:
            score = 0
            for letter in word:
                if letter == 'c':
                    score += 1
                else:
                    score -= 1
            c.append(score)
        c.sort(reverse=True)
        still = 0
        cnt = 0
        for j in range(len(c)):
            if still + c[j] > 0:
                still += c[j]
                cnt += 1
            else:
                break
        answer = max(answer, cnt)
        d = []
        for word in words:
            score = 0
            for letter in word:
                if letter == 'd':
                    score += 1
                else:
                    score -= 1
            d.append(score)
        d.sort(reverse=True)
        still = 0
        cnt = 0
        for j in range(len(d)):
            if still + d[j] > 0:
                still += d[j]
                cnt += 1
            else:
                break
        answer = max(answer, cnt)
        e = []
        for word in words:
            score = 0
            for letter in word:
                if letter == 'e':
                    score += 1
                else:
                    score -= 1
            e.append(score)
        e.sort(reverse=True)
        still = 0
        cnt = 0
        for j in range(len(e)):
            if still + e[j] > 0:
                still += e[j]
                cnt += 1
            else:
                break
        answer = max(answer, cnt)
        return answer
        pass
    t = int(input())
    ans = []
    for _ in range(t):
        n = int(input())
        words = []
        for i in range(n):
            words.append(input()[:-1])
        ans.append(solve(n, words))
    for answer in ans:
        print(answer)
if __name__ == '__main__':
    import sys, threading
    import bisect
    import math
    import itertools
    from sys import stdout
    import heapq
    from queue import PriorityQueue
    input = sys.stdin.readline
    thread = threading.Thread(target=main)
    thread.start()
    thread.join()