This page is still under construction.

Parts of this page are still being built. What you see may change.

Palindromes

Time limit5sMemory limit256 MB

Summary
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 nn palindromes (a palindrome is a word that reads the same forwards and backwards, such as dad, eye, or racecar). He then formed all n2n^2 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 nn (n≥2n \ge 2), the number of palindromes Johnny has chosen. Each of the following nn lines describes one palindrome: the (i+1)(i+1)-st line contains a positive integer aia_i, the length of the ii-th palindrome, followed by a single space and a palindrome consisting of aia_i 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 (i,j)(i, j) may have i=ji = j, and when i≠ji \ne j the pairs (i,j)(i, j) and (j,i)(j, i) are counted separately.

Examples1

  1. Example 1

    Input
    6
    2 aa
    3 aba
    3 aaa
    6 abaaba
    5 aaaaa
    4 abba
    
    Expected output
    14