Under construction

Still being built. Things may change

Confusing Login Names

Time limit3sMemory limit128 MB

Summary
Compute an extended edit distance (insert, delete, replace, adjacent swap) between all pairs of given login names and output pairs within a given threshold, sorted alphabetically.
Level

Medium6 of 10

Topics
Dynamic programming, String, String matching
Solved
No attempts yet

Problem

Meikyokan University is very famous for its research and education in computer science. It has a computer center with advanced and secure computing facilities, including supercomputers and many personal computers connected to the Internet.

One policy of the computer center is to let students choose their own login names. Unfortunately, students tend to choose similar login names, and mistakes when typing or specifying a login name are relatively common. Such troubles are a burden on the center's staff.

To avoid them, Dr. Choei Takano, the chief manager of the computer center, decided to eliminate similar, confusing login names. To do so, Takano must develop a program that detects confusing login names.

Based on the following four operations on strings, the distance between two login names is defined as the minimum number of operations that transforms one login name into the other.

  1. Deleting a character at an arbitrary position.
  2. Inserting a character at an arbitrary position.
  3. Replacing a character at an arbitrary position with another character.
  4. Swapping two adjacent characters at an arbitrary position.

For example, the distance between "omura" and "murai" is two, because the following sequence of operations transforms "omura" into "murai".

omura → (delete 'o') → mura → (insert 'i') → murai

As another example, the distance between "akasan" and "kaason" is also two.

akasan → (swap 'a' and 'k') → kaasan → (replace 'a' with 'o') → kaason

Takano decided that two login names with a small distance are confusing and must be avoided.

Your job is to write a program that enumerates all confusing pairs of login names.

Beware that the rules may combine in subtle ways. For instance, the distance between "ant" and "neat" is two.

ant → (swap 'a' and 'n') → nat → (insert 'e') → neat

Input

The input consists of multiple datasets. Each dataset is given in the following format.

n
d
name1
name2
···
namen

The first integer nn is the number of login names. Then comes a positive integer dd. Two login names whose distance is at most dd are deemed confusing. You may assume that 0<n≤2000 < n \le 200 and 0<d≤20 < d \le 2. The ii-th student's login name is given by namei\text{name}_i, which consists only of lowercase letters and has length less than 1616. You may assume that the names namei\text{name}_i (1≤i≤n1 \le i \le n) are all distinct.

The end of the input is indicated by a line that contains only a single zero.

Output

For each dataset, your program should output all pairs of confusing login names, one pair per line, followed by the total number of confusing pairs in the dataset.

In each pair, the two login names are separated by a single comma character (,), and the login name that alphabetically precedes the other appears first. The entire output of confusing pairs for each dataset must be sorted as follows: for two pairs "w1,w2" and "w3,w4", if w1 alphabetically precedes w3, or they are equal and w2 precedes w4, then "w1,w2" must appear before "w3,w4".

Examples1

  1. Example 1

    Input
    8
    2
    omura
    toshio
    raku
    tanaka
    imura
    yoshoi
    hayashi
    miura
    3
    1
    tasaka
    nakata
    tanaka
    1
    1
    foo
    5
    2
    psqt
    abcdef
    abzdefa
    pqrst
    abdxcef
    0
    
    Expected output
    imura,miura
    imura,omura
    miura,omura
    toshio,yoshoi
    4
    tanaka,tasaka
    1
    0
    abcdef,abdxcef
    abcdef,abzdefa
    pqrst,psqt
    3