Similar Rhyme Game
InterviewTime limit1sMemory limit512 MB
Given n distinct words, pair them up so that each pair shares a common suffix of length at least F, maximizing the number of pairs.
Problem
Alice enjoys a word game in which she combines English words to make rhymes. Bob also wants to make rhymes, but he is young and does not know many words, so Alice proposes the "Similar Rhyme Game."
First, write distinct words of length consisting only of uppercase English letters on paper. For convenience, call these words .
When comparing a pair of words and , if the length of their longest common suffix is at least , the two words are said to form a "similar rhyme." Such a pair of words is called a "similar rhyme pair."
The "Similar Rhyme Game" is a game of forming as many "similar rhyme pairs" as possible from the given words. Each word may belong to at most one similar rhyme pair.
For example, let and , with the four words "WALK", "TALK", "MILK", "BULK".
- If , pairing the words into any two pairs gives similar rhyme pairs, so pairs can be found. For example, ("
WALK", "BULK") and ("TALK", "MILK"). - If , pairing ("
WALK", "TALK") forms one similar rhyme pair, but there is no way to form or more. - If , there is no way to form a similar rhyme pair.
Given and words of length as input, find the maximum number of "similar rhyme pairs" Bob can form.
Input
The first line gives the number of test cases .
The input for each test case is given over two lines. The first line gives , , and , separated by spaces. The second line gives strings of length consisting only of uppercase English letters, separated by spaces.
Output
For each test case, print the answer on its own line.
Constraints
- Each string consists only of uppercase English letters.
- The strings given in each test case are distinct.
Hint
Longest common suffix: the longest common suffix of two strings, that is, the common suffix with the greatest length.