当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

2146: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.
1. Deleting a character at an arbitrary position.
2. Inserting a character into 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" 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


输入解释
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.
输出解释
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".
输入样例
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
输出样例
imura,miura
imura,omura
miura,omura
toshio,yoshoi
4
tanaka,tasaka
1
0
abcdef,abdxcef
abcdef,abzdefa
pqrst,psqt
3

该题目是Virtual Judge题目,来自 北京大学POJ

题目来源 Japan 2004

源链接: POJ-2146

最后修改于 2020-10-29T06:24:37+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
10000 65536