UberOA #65
Word Pairs
Estimated MediumJudge-ready
Topics (AI-labelled)Trie & CodecHashing
Count unordered pairs of distinct indices i<j such that the two words are equal, or one is a prefix of the other. Identical words at different indices are still counted separately.
This site has made the standard input and output explicit.
Input
The first line contains n, followed by n words. The source gives no character or length bounds; on this site 1≤n≤100000, and words contain only lowercase English letters with length 1..30.
Output
Output one integer, the answer.
Example 1
Input:
7
back
backdoor
gammon
backgammon
comeback
come
door
Output:
3
Example 2
Input:
6
abc
a
a
b
ab
ac
Output:
8
Example 3
Input:
3
x
x
x
Output:
3
Example notes
Example 1: back pairs with backdoor and backgammon, 2 pairs, and come pairs with comeback, 1 pair, for 3 in total. Example 2: the two a's form 1 pair with each other, and each forms pairs with abc, ab and ac, giving 6 pairs; ab and abc form 1 more, for 8 in total. Example 3: three distinct indices pair up with each other, for 3 in total.
Editorial
Loading solution…