Robot Vacuum Cleaner (Codeforces 922D)
Tags: Greedy, sortings

Pushok the dog has been chasing Imp for a few hours already.  Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and  and . The robot is off at the moment. Imp knows that it has a sequence of strings ti in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.Help Imp to find the maximum noise he can achieve by changing the order of the strings.

Input: The first line contains a single integer n (1 ≤ n ≤ 105) — the number of strings in robot's memory.Next n lines contain the strings t1, t2, ..., tn, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 105.

Output: Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.

Note: The optimal concatenation in the first sample is ssshhshhhs.

Sample Inputs:
4
ssh
hs
s
hhhs

2
h
s

Sample Outputs:
18

1

Solution (Python):
import sys
input = lambda : sys.stdin.readline().strip()

def I():
    return input()

def II():
    return int(input())

def MII():
    return map(int, input().split())

def LI():
    return list(input().split())

def LII():
    return list(map(int, input().split()))

def LFI():
    return list(map(float, input().split()))

def GMI():
    return map(lambda x: int(x) - 1, input().split())

def LGMI():
    return list(map(lambda x: int(x) - 1, input().split()))

def main():
    s = []
    for _ in range(int(input())):
        s.append(input())

    def f(s):
        return s.count('s') / len(s)
    s.sort(reverse=True, key=lambda x: f(x))
    fores = 0
    ans = 0
    for i in ''.join(s):
        if i == 's':
            fores += 1
        else:
            ans += fores
    print(ans)
    return
main()

Solution (Python):
n = int(input())
t = []
(extra_s, extra_h) = (0, 0)

def calcLoudness(string):
    (value, h_count) = (0, 0)
    for char in string:
        if char == 'h':
            h_count += 1
    for chara in string:
        if chara == 'h':
            h_count -= 1
        if chara == 's':
            value += h_count
    return value
for _ in range(n):
    string = input()
    s_count = 0
    for chara in string:
        if chara == 's':
            s_count += 1
    if s_count == 0:
        extra_h += len(string) - s_count
    elif s_count < len(string):
        t.append([string, s_count / (len(string) - s_count)])
    else:
        extra_s += s_count
t = sorted(t, key=lambda x: x[1], reverse=True)
O = 's' * extra_s
for x in t:
    O += x[0]
O += 'h' * extra_h
print(calcLoudness(O))

Solution (Python):
from math import inf

def solve(arr) -> int:

    def f(s):
        sh = 0
        h = 0
        for c in reversed(s):
            if c == 'h':
                h += 1
            else:
                sh += h
        return sh
    arr.sort(key=lambda x: -x.count('s') / x.count('h') if x.count('h') else -inf)
    arrs = ''.join(arr)
    return f(arrs)

def main():
    n = int(input())
    arr = []
    for _ in range(n):
        arr.append(input())
    print(solve(arr))

def init():
    import sys
    import os
    if 'CP_LOCAL_ENV' in os.environ:
        sys.stdin = open('test.txt')
if __name__ == '__main__':
    init()
    main()