Problem:
Problem E: Confusing Login Names

Meikyokan University is very famous for its research and education in the area of computer
science. This university has a computer center that has advanced and secure computing facilities
including supercomputers and many personal computers connected to the Internet.

One of the policies of the computer center is to let the students select their own login names.
Unfortunately, students are apt to select similar login names, and troubles caused by mistakes
in entering or specifying login names are relatively common. These troubles are a burden on
the staff of the computer center.

To avoid such troubles, Dr. Choei Takano, the chief manager of the computer center, decided
to stamp out similar and confusing login names. To this end, Takano has to develop a program
that detects confusing login names.

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

Deleting a character at an arbitrary position.

Inserting a character into an arbitrary position.

Replacing a character at an arbitrary position with another character.

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” to “murai”.

delete ‘o’ insert ‘i’
omura --> mura --> murai

Another example is that the distance between “akasan” and “kaason” is also two.

swap ‘a’ and ‘k’ replace ‘a’ with ‘o’
akasan --> kaasan --> kaason

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

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

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

swap ‘a’ and ‘n’ insert ‘e’
ant --> nat --> neat

Input

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

n
d
name1
name2
...
namen

The first integer n is the number of login names. Then comes a positive integer d. Two login
names whose distance is less than or equal to d are deemed to be confusing. You may assume
that 0 < n ≤ 200 and 0 < d ≤ 2. The i-th student’s login name is given by namei, which is
composed of only lowercase letters. Its length is less than 16. You can assume that there are no
duplicates in namei (1 ≤ i ≤ n).

The end of the input is indicated by a line that solely contains a 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 to be separated only by a comma character (,), and the
login name that is alphabetically preceding the other should appear 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 the same and w2 precedes w4, then “w1,w2” must
appear before “w3,w4”.

Sample 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

Output for the Sample Input

imura,miura
imura,omura
miura,omura
toshio,yoshoi
4
tanaka,tasaka
1
0
abcdef,abdxcef
abcdef,abzdefa
pqrst,psqt
3

Solution in C (judge verdict: Accepted)

// AOJ 1252: Confusing Login Names
// 2017.10.12 bal4u@uu

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ABS(a) ((a)>=0?(a):-(a))

typedef struct { char *s; int w; } T;
T nm[202];
char name[202][18];
int M[20][20];

int cmp(T *a, T *b) { return strcmp(a->s, b->s); }

int distance(char *s1, int w1, char *s2, int w2)
{
    int  i, j;

    for (j = 0; j <= w1; j++) M[j][0] = j;
    for (i = 0; i <= w2; i++) M[0][i] = i;
    for (j = 1; j <= w1; j++) {
        for (i = 1; i <= w2; i++) {
            int d, min;
            d = (s1[j-1] != s2[i-1]);
            min =     M[j-1][i  ] + 1;
            if (min > M[j  ][i-1] + 1) min = M[j  ][i-1] + 1;
            if (min > M[j-1][i-1] + d) min = M[j-1][i-1] + d;
            M[j][i] = min;
        }
    }
    return M[w1][w2];
}

int check(char *s1, int w1, char *s2, int w2, int d)
{
	char *t1, *t2;

	if (ABS(w1-w2) > d) return 0;
	if (distance(s1, w1, s2, w2) <= d) return 1;

	t1 = s1, t2 = s2;
	if (d == 1) {
		if (w1 != w2) return 0;
		while (*s1 == *s2) s1++, s2++;
		if (*s1 == *(s2+1) && *s2 == *(s1+1)) return strcmp(s1+2, s2+2) == 0;
		return 0;
	}

	s1 = t1, s2 = t2; while (*s1 == *s2) s1++, s2++;
	if (*s1 == *(s2+1) && *s2 == *(s1+1)) {
		s1 += 2, s2 += 2;
		if (distance(s1, w1-(s1-t1), s2, w2-(s2-t2)) < d) return 1;
		if (w1 != w2) return 0;
		while (*s1 == *s2) s1++, s2++;
		if (*s1 == *(s2+1) && *s2 == *(s1+1)) return strcmp(s1+2, s2+2) == 0;
		return 0;
	}
	if (*s1 == *(s2+1) && *s2 == *(s1+2)) return strcmp(s1+3, s2+2) == 0;
	if (*s1 == *(s2+2) && *s2 == *(s1+1)) return strcmp(s1+2, s2+3) == 0;

	s1 = t1+w1, s2 = t2+w2;
	while (*s1 == *s2) s1--, s2--;
	if (s2 > t2 && s1 > t1 && *s1 == *(s2-1) && *s2 == *(s1-1)) {
		s1 -= 2, s2 -= 2;
		return distance(t1, s1-t1+1, t2, s2-t2+1) < d;
	}
	return 0;
}

int main()
{
	int n, d, i, j, ans;
	char buf[22];

	while (fgets(buf, 20, stdin) && *buf != '0') {
		n = atoi(buf);
		fgets(buf, 20, stdin), d = atoi(buf);
		for (i = 0; i < n; i++) {
			fgets(name[i], 17, stdin);
			nm[i].s = name[i], nm[i].w = strlen(name[i])-1, name[i][nm[i].w] = 0;
		}
		qsort(nm, n, sizeof(T), cmp);
		for (ans = 0, i = 0; i < n; i++) for (j = i+1; j < n; j++) {
			if (check(nm[i].s, nm[i].w, nm[j].s, nm[j].w, d)) {
				printf("%s,%s\n", nm[i].s, nm[j].s), ans++;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}