暴搜。。。HOJ2297 Automatic Correction of Misspellings。

暴力过的。。 与限时只差0.03s。。。

Automatic Correction of Misspellings


Time limit:2sec.Submitted:30
Memory limit:64MAccepted:17
Source: Ulm 2006

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:

  1. is correct, if the word occurs in the dictionary.
  2. is a misspelling of <x>, where <x> 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.
  3. is unknown, if cases 1 and 2 do not apply.

Sample Input

10
this
is
a
dictionary
that
we
will
use
for
us
6
su
as
the
dictonary
us
willl

Sample Output

su is a misspelling of us
as is a misspelling of is
the is unknown
dictonary is a misspelling of dictionary
us is correct
willl is a misspelling of will


RunidProidSubtimeJudgestatusRuntimeMemoryLanguageCode LengthAuthor
58956422972011-06-15 16:43:53Accepted1.97 s1692 KC++2116 Byangchenhao8

说明:
囧。。。题意是字典找最接近的词。 包括一次相邻调换、换一个字母、多一个或少一个字母。
如果有多种 misspelling的可能 。  输出字典中第一次出现的词 。
思路。。对每一个输入的词。 对字典中每个词找到第一次不同的字母位置。 然后做处理。。。
是有多暴力。。

代码如下:
 
  
#include < iostream >
#include
< algorithm >
#include
< cstring >
#include
< vector >
using namespace std;

int main() {
int n;
cin
>> n;
vector
< string > dic;
string tmp;
while (n -- ) {
cin
>> tmp;
dic.push_back(tmp);
}
int p;
cin
>> p;
while (p -- ) {
cin
>> tmp;
int i, j;
string mfs;
bool cf = 0 , mf = 0 ;
for (i = dic.size() - 1 ; i >= 0 ; i -- ) {
int l1 = dic[i].length(), l2 = tmp.length();
if (tmp == dic[i]) {
cf
= 1 ;
break ;
}
else if (l1 == l2) {
for (j = 0 ;; j ++ ) {
if (tmp[j] != dic[i][j])
break ;
}
string tmp1 = tmp;
if (l1 != 1 ) {
swap(tmp1[j], tmp1[j
+ 1 ]);
if (tmp1 == dic[i]) {
mf
= 1 ;
mfs
= dic[i];
}
}
tmp1
= tmp;
tmp1[j]
= dic[i][j];
if (tmp1 == dic[i]) {
mf
= 1 ;
mfs
= dic[i];
}
}
else if (abs(l1 - l2) == 1 ) {
string tmp1 = tmp, tmp2 = dic[i];
if (tmp1.length() > tmp2.length())
tmp1.swap(tmp2);
for (j = 0 ;; j ++ ) {
if (tmp2[j] != tmp1[j])
break ;
}
string t = "" ;
t
+= tmp2[j];
tmp1.insert(j, t);
if (tmp1 == tmp2) {
mf
= 1 ;
mfs
= dic[i];
}
}
}
if (cf) {
cout
<< tmp << " is correct\n " ;
}
else if (mf) {
cout
<< tmp << " is a misspelling of " << mfs << endl;
}
else cout << tmp << " is unknown\n " ;
}
return 0 ;
}

转载于:https://www.cnblogs.com/yangchenhao8/archive/2011/06/15/2081649.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值