Spell checker
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24755 Accepted: 9055
Description
You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms.
If the word is absent in the dictionary then it can be replaced by correct words (from the dictionary) that can be obtained by one of the following operations:
?deleting of one letter from the word;
?replacing of one letter in the word with an arbitrary letter;
?inserting of one arbitrary letter into the word.
Your task is to write the program that will find all possible replacements from the dictionary for every given word.
Input
The first part of the input file contains all words from the dictionary. Each word occupies its own line. This part is finished by the single character ‘#’ on a separate line. All words are different. There will be at most 10000 words in the dictionary.
The next part of the file contains all words that are to be checked. Each word occupies its own line. This part is also finished by the single character ‘#’ on a separate line. There will be at most 50 words that are to be checked.
All words in the input file (words from the dictionary and words to be checked) consist only of small alphabetic characters and each one contains 15 characters at most.
Output
Write to the output file exactly one line for every checked word in the order of their appearance in the second part of the input file. If the word is correct (i.e. it exists in the dictionary) write the message: ” is correct”. If the word is not correct then write this word first, then write the character ‘:’ (colon), and after a single space write all its possible replacements, separated by spaces. The replacements should be written in the order of their appearance in the dictionary (in the first part of the input file). If there are no replacements for this word then the line feed should immediately follow the colon.
Sample Input
i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#
Sample Output
me is correct
aware: award
m: i my me
contest is correct
hav: has have
oo: too
or:
i is correct
fi: i
mre: more me
Source
Northeastern Europe 1998
题意:
首先给你字典中的一些单词,以#表示结束。然后再给你一些待查询的单词,以#结尾,如果该单词在字典中则输出“。。。 is correct”,否则按照顺序输出字典中的单词当且仅当该字典单词可以通过删除,替换,或插入一个字母使他们一样。
分析:
由于字典最多1000个单词,待查单词最多50个,且每个单词最多15个字母,所以我们可以直接暴力。写三个函数分别判断删除,插入,和替换即可。
在检查替换时我们可以顺便检查两个单词是否相等。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
char dic[10005][20];
char w[55][20];
int ok[10005];
int n,m,flag;
bool del(char* w,char* d)//删除
{
int len1 = strlen(w);
int len2 = strlen(d);
if(len2-len1!=1) return false;
int i,j;
for(i=0;i<len2;i++)
{
if(d[i]!=w[i]) break;
}
j=i;i++;
for(;i<len2;i++,j++)
{
if(d[i]!=w[j]) break;
}
if(i>=len2) return true;
else return false;
}
bool ins(char* w,char* d)//插入
{
int len1 = strlen(w);
int len2 = strlen(d);
if(len1-len2!=1) return false;
int i,j;
for(i=0;i<len1;i++)
{
if(w[i]!=d[i]) break;
}
j=i;i++;
for(;i<len1;i++,j++)
{
if(d[j]!=w[i]) break;
}
if(i>=len1) return true;
else return false;
}
bool rep(char* w,char* d)//替换
{
int len1 = strlen(w);
int len2 = strlen(d);
if(len1!=len2) return false;
int i;
for(i=0;i<len1;i++)
{
if(w[i]!=d[i]) break;
}
if(i>=len1)//两个单词相等
{
flag=1;
return true;
}
i++;
for(i;i<len1;i++)
{
if(w[i]!=d[i]) break;
}
if(i>=len1) return true;
else return false;
}
int main()
{
n=m=0;
while(scanf("%s",dic[n]))
{
if(dic[n][0]=='#') break;
n++;
}
while(scanf("%s",w[m]))
{
if(w[m][0]=='#') break;
m++;
}
for(int i=0;i<m;i++)
{
int q = 0;
flag = 0;
for(int j=0;j<n;j++)
{
if(del(w[i],dic[j]) || rep(w[i],dic[j]) || ins(w[i],dic[j])) ok[q++]=j;
if(flag) break;
}
if(flag) printf("%s is correct\n",w[i]);
else
{
printf("%s:",w[i]);
for(int k=0;k<q;k++)
{
printf(" %s",dic[ok[k]]);
}
printf("\n");
}
}
return 0;
}