H - Yet Another Sigma Problem Editorial by toam


文字列を hash 化することでも解けます.

実装例 (Python)

from collections import defaultdict

n = int(input())
s = list(input().split())

cnt = defaultdict(int)
B = 200
mod = 10**16 + 61
ans = 0
for si in s:
    hash = 0
    for j in si:
        hash = (hash * B + ord(j)) % mod
        ans += cnt[hash]
        cnt[hash] += 1

print(ans)

posted:
last update: