Spell checker

 
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22541   Accepted: 8220

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
用单词的长度做hash,刚开始用C++写超时,后改成C便AC了。
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 char *dic[10000];
  5 char dirc[18][10000][18];
  6 int rank1[17];
  7 int rank2[17][10000];
  8 int cmp ( const void *a , const void *b)
  9 {
 10         return *(int *)a - *(int *)b;
 11 }
 12 void findRight(char*  str){
 13             int i=strlen(str);
 14             int flag=0;
 15             int result[10000];
 16             memset(result,0,sizeof(result));
 17             int dsize;
 18             int count=0;
 19             dsize=rank1[i];
 20             for(int k=0;k<dsize;k++){
 21                 char* str2=dirc[i][k];
 22                 int diff=0;
 23                 for(int m=0;m<i;m++){
 24                     if(str[m]!=str2[m]){
 25                         diff++;
 26                     }
 27                 }
 28                 if(diff==1)
 29                     result[count++]= rank2[i][k];
 30                 else if(diff==0){
 31                     flag=1;
 32                     break;
 33                 }
 34             }
 35             if(!flag){
 36                 dsize=rank1[i-1];
 37                 for(int k=0;k<dsize;k++){
 38                     char* str2=dirc[i-1][k];
 39                     int diff=0;
 40                     int m=0,n=0;
 41                     for(;n<i&&m<i-1;){
 42                         if(str[n]!=str2[m]){
 43                             diff++;
 44                             if(diff>=2)
 45                                 break;
 46                             n++;
 47                         }else{
 48                             m++;
 49                             n++;
 50                         }
 51 
 52                     }
 53                     diff+=strlen(str2)-m;
 54                     if(diff<=1)
 55                         result[count++]=rank2[i-1][k];
 56                 }
 57                 dsize=rank1[i+1];
 58                 for(int k=0;k<dsize;k++){
 59                     char* str2=dirc[i+1][k];
 60                     int diff=0;
 61                     int m=0,n=0;
 62                     for(;n<i&&m<i+1;){
 63 
 64                         if(str[n]!=str2[m]){
 65                             diff++;
 66                             if(diff>=2)
 67                                 break;
 68                             m++;
 69                         }else{
 70                             m++;
 71                             n++;
 72                         }
 73 
 74                     }
 75                     diff+=i-n;
 76                     if(diff<=1)
 77                         result[count++]=rank2[i+1][k];
 78                 }
 79 
 80             }
 81             if(flag)
 82                 printf("%s is correct
",str);
 83             else{
 84                 if(count>0)
 85                 qsort(result,count,sizeof(int),cmp);
 86                 printf("%s: ",str);
 87                 for(int k=0;k<count;k++){
 88                     printf("%s ",dic[result[k]]);
 89                 }
 90                 printf("
");
 91             }
 92 
 93 }
 94 
 95 int main() {
 96     char word[18];
 97     int r=0;
 98     memset(rank1,0,sizeof(rank1));
 99     while(gets(word)){
100         if(word[0]=='#')
101             break;
102         int len=strlen(word);
103         strcpy(dirc[len][rank1[len]],word);
104         dic[r]=dirc[len][rank1[len]];
105         rank2[len][rank1[len]]=r;
106         r++;
107         rank1[len]++;
108     }
109     while(gets(word)){
110         if(word[0]=='#')
111             break;
112         findRight(word);
113     }
114     return 0;
115 }

下面是用C++写的超时版本

  1 #include <iostream>
  2 #include<vector>
  3 #include<algorithm>
  4 using namespace std;
  5 vector<string> directory[17];
  6 vector<string> dir;
  7 vector<int> rank[17];
  8 
  9 vector<string> input[16];
 10 void findRight(string str){
 11             int i=str.length();
 12             bool flag=0;
 13             vector<int> result;
 14             int dsize;
 15             dsize=directory[i].size();
 16             for(int k=0;k<dsize;k++){
 17                 string str2=directory[i][k];
 18                 int diff=0;
 19                 for(int m=0;m<i;m++){
 20                     if(str[m]!=str2[m]){
 21                         diff++;
 22                     }
 23                 }
 24                 if(diff==1)
 25                     result.push_back(rank[i][k]);
 26                 else if(diff==0){
 27                     flag=1;
 28                     break;
 29                 }
 30             }
 31             if(!flag){
 32                 dsize=directory[i-1].size();
 33                 for(int k=0;k<dsize;k++){
 34                     string str2=directory[i-1][k];
 35                     int diff=0;
 36                     int m=0,n=0;
 37                     for(;n<i&&m<i-1;){
 38                         if(str[n]!=str2[m]){
 39                             diff++;
 40                             if(diff>=2)
 41                                 break;
 42                             n++;
 43                         }else{
 44                             m++;
 45                             n++;
 46                         }
 47 
 48                     }
 49                     diff+=str2.length()-m;
 50 
 51                     if(diff<=1)
 52                         result.push_back(rank[i-1][k]);
 53                 }
 54                 dsize=directory[i+1].size();
 55                 for(int k=0;k<dsize;k++){
 56                     string str2=directory[i+1][k];
 57                     int diff=0;
 58                     int m=0,n=0;
 59                     for(;n<i&&m<i+1;){
 60 
 61                         if(str[n]!=str2[m]){
 62                             diff++;
 63                             if(diff>=2)
 64                                 break;
 65                             m++;
 66                         }else{
 67                             m++;
 68                             n++;
 69                         }
 70 
 71                     }
 72                     diff+=str.length()-n;
 73                     if(diff<=1)
 74                         result.push_back(rank[i+1][k]);
 75                 }
 76 
 77             }
 78             if(flag)
 79                 cout<<str<<" is correct"<<endl;
 80             else{
 81                 dsize=result.size();
 82                 sort(result.begin(),result.end());
 83                 cout<<str<<": ";
 84                 for(int k=0;k<dsize;k++){
 85                     cout<<dir[result[k]]<<' ';
 86                 }
 87                 cout<<endl;
 88             }
 89 
 90 }
 91 
 92 int main() {
 93     string str;
 94     int r=0;
 95     while(cin>>str){
 96         if(str=="#")
 97             break;
 98         directory[str.length()].push_back(str);
 99         dir.push_back(str);
100         rank[str.length()].push_back(r++);
101 
102     }
103     while(cin>>str){
104         if(str=="#")
105             break;
106         findRight(str);
107     }
108     return 0;
109 }
原文地址:https://www.cnblogs.com/sdxk/p/4682589.html