P7756 [COCI 2012/2013 #3] MALCOLM

Background

Ever since Teacher Herkabe started ranking his students, the number of friends in his class has dropped sharply. Students with lower ranks have begun to envy students with higher ranks, and students with higher ranks have started to look down on those with lower ranks.

Description

According to Malcolm’s observations, there are $n$ students in the class. If the ranks of two students differ by **at most $k$**, then they are **friends**. If two students are **friends** and their name lengths are the same, then they are **good friends**. Now you are given the names and ranks of these $n$ students. Find how many pairs of **good friends** there are in the class.

Input Format

The input has $n+1$ lines. The first line contains two integers $n,k$, representing the number of students in the class and the maximum rank difference allowed for being friends. The next $n$ lines follow: the $(i+1)$-th line contains a string, the name of the student ranked $i$-th in the class.

Output Format

Output a single line with one integer, the number of pairs of **good friends** in the class.

Explanation/Hint

**Constraints** For all testdata, $3\leqslant n\leqslant 3\times 10^5$, $1\leqslant k\leqslant n$. Each string has length in $[2,20]$ and contains only uppercase English letters. **Source** This problem is from **_[COCI 2012-2013](https://hsin.hr/coci/archive/2012_2013/) [CONTEST 3](https://hsin.hr/coci/archive/2012_2013/contest3_tasks.pdf) T3 MALCOLM_**. Using the original testdata settings, the full score is $100$ points. Translated and organized by [Eason_AC](https://www.luogu.com.cn/user/112917). Translated by ChatGPT 5