Palindromes
Time limit5sMemory limit256 MB
Given n distinct palindromes, count ordered pairs whose concatenation is also a palindrome, with total length up to 2,000,000.
- Level
Medium7 of 10
- Topics
- String, Hash map, String matching, Prefix sum
- Solved
- No attempts yet
Problem
Little Johnny enjoys playing with words. He has chosen palindromes (a palindrome is a word that reads the same forwards and backwards, such as dad, eye, or racecar). He then formed all ordered pairs of them and, for each pair, concatenated the two palindromes in order into a single word. Finally, he counted how many of the resulting words are themselves palindromes. Johnny is not sure he did this without a mistake, so he has asked you to repeat exactly the same steps and report the result.
Write a program which:
- reads Johnny's palindromes from the standard input,
- determines how many of the words formed by concatenating an ordered pair of the input palindromes are themselves palindromes,
- writes the result to the standard output.
Input
The first line of the standard input contains a single integer (), the number of palindromes Johnny has chosen. Each of the following lines describes one palindrome: the -st line contains a positive integer , the length of the -th palindrome, followed by a single space and a palindrome consisting of lowercase English letters. The palindromes on different lines are all distinct. The total length of all palindromes does not exceed 2,000,000.
Output
Print a single integer on the first line: the number of ordered pairs of palindromes whose concatenation is itself a palindrome. A pair may have , and when the pairs and are counted separately.