HDU1483《Automatic Correction of Misspellings》
Problem Description
Some text editors offer a feature to correct words which seem to be written incorrectly. In this problem you are asked to implement a simple Automatic Correction of Misspellings (ACM).
ACM takes care of the following misspellings of words:
1.One letter is missing (e.g., letter is written leter) or too much (e.g., letter is written lettter).
2.One letter is wrong (e.g., letter is written ketter)
3.The order of two adjacent letters is wrong (e.g., letter is written lettre)
ACM is based on a dictionary of known words. When a text contains a word which is not in the dictionary, ACM will try to replace it by a similar word of the dictionary. Two words are similar if we can transform one word into the other by doing exactly one of the misspellings listed above. An unknown word is left unchanged if there is no similar word in the dictionary.
Input
The first line of the input file will give the number n of words in the dictionary (n ≤ 10000). The next n lines contain the dictionary words. The following line contains an integer q ≤ 1000, the number of query words. The next q lines contain the query words. You may assume that each word in the input consists of 1 to 25 lower case letters (‘a’ to ‘z’).
Output
For each query word, print one line with the query word followed by one of the following possibilities:
- is correct, if the word occurs in the dictionary.
- is a misspelling of
, where is a word of the dictionary similar to the query word, and the query word is not in the dictionary. In the case that there are several possibilities, select the word from the dictionary which appeared earlier in the input. - is unknown, if cases 1 and 2 do not apply.
Sample Input
1 |
|
Sample Output
1 |
|
解析
还是从代码里复制过来的(
场上写一个 Trie 写到自闭。。。
赛后来补一下
道理我都懂,但是这题为什么暴力能过。。。
以下,将字典中的串称作「字典串」,将询问的串称作「匹配串」
首先扫一遍字典,有相同的直接输出 correct
然后如果没有相同的,再扫一遍字典,对于每一个字典串,做这样几件事:
- 如果字典串和匹配串长度相等,就说明这个匹配串可能是当前字典串的一个错误拼写
逐位扫一遍两个串,记一下错误的次数,以及最后一次错误的下标
如果错误次数是 1 就直接输出 misspelling,此时匹配串相对于字典串只错了一个字符
如果错误次数是 2 就判断一下是否是顺序弄反了,这个用最后一次错误下标很好写,如果是就输出 misspelling
否则就凉凉 - 如果字典串比匹配串长 1,就说明这个匹配串可能是当前字典串漏了一个字
逐位扫一遍字典串,用一个变量 k 记录当前字典串这一位对应的是匹配串的哪一位
如果当前的两个串对应字符相等,就让 k 正常加一,否则就不让 k 加一
显然如果真的是只漏了一个字,那么最后 k 一定等于匹配串长度,输出 misspelling
否则 k 一定不等于匹配串长度(具体会变成什么值我也不大清楚,反正模拟一下就好了) - 如果字典串比匹配串短 1,就说明这个匹配串可能是当前字典串添了一个字
仿照着情况 2 做就完事了
最后如果三种情况都没有,输出 unknown
代码实现
1 |
|
- 本文作者:Handwer STD
- 本文链接:https://blog.handwer-std.top/2019-08-08/HDU1483/index.html
- 版权声明:本博客所有文章均采用 BY-NC-SA 许可协议,转载请注明出处!