Minheejin
InterviewTime limit1sMemory limit512 MB
Given N names, count the pairs of people where a non-empty suffix of one name matches a prefix of the other.
- Level
Easy3 of 10
- Topics
- String, String matching, Brute force
- Solved
- No attempts yet
Problem
Connecting Lee Minhee and Minhee Jin gives Lee Minheejin
Connecting Kim Seoin and Seo-in Guk gives Kim Seoin-guk
Connecting Park Geon and Geonppang gives Park Geonppang
Minhee enjoys connecting two people when the back part of one person's name matches the front part of the other person's name.
Given people, find the number of distinct pairs of people that can be connected.
Two people with names and can be connected if there is a positive integer with the length of and the length of such that either the first letters of match the last letters of , or the last letters of match the first letters of .
Input
The first line contains the number of people .
From the second line onward, each of the next lines contains one person's name.
Each name consists only of lowercase English letters and has a length of at least 1 and at most 20.
Some people may share the same name.
Output
On the first line, print the number of distinct pairs of people that can be connected.