[ACM] POJ 1035 Spell checker (单词查找,删除替换增加任何一个字母)

来源:互联网 发布:毛丝鼠淘宝 编辑:程序博客网 时间:2024/05/19 09:50
Spell checker
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 18693 Accepted: 6844

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

iishashavebemymorecontestmetooifaward#meawaremcontesthavooorifimre#

Sample Output

me is correctaware: awardm: i my mecontest is correcthav: has haveoo: tooor:i is correctfi: imre: more me

Source

Northeastern Europe 1998


解题思路:

输入词典中的单词,再输入需要查找的单词,查找到有四种方法:

1.  该单词存在词典中

2.该单词通过替换任何一个字母后存在于词典中(如果有多个,按词典序输出)

3.该单词通过删除任何一个字母后存在于词典中

4.该单词通过增加一个字母后存在于词典中

做本题遇到的问题:

char dic[ ] [ ] 存词典,  char wor[] [] 存待查找的单词。

在判断单词是否能通过删除一个字母后存在于词典中时,因为函数传的是指针,而函数中又把字母的顺序移动了,这就造成把原单词给改变了,这个错误太致命了。。。其实判断上面说的后两种情况可以通过指针移动,判断不匹配的个数,单词的指针为i, 词典某个单词的指针为j ,  当它们的字母相同时,i++,j++,不同时, 就让单词长度长的那个指针+1,mistake+1另一个不动,mistake的个数不能大于1,(因为判断的两个单词长度之差的绝对值不会大于1).

还有有个地方严重超时,下面代码:

[cpp] view plaincopy
  1. bool replace(char *a,char *b)  
  2. {  
  3.     string s1=a,s2=b;  
  4.     int len1=s1.length();  
  5.     int mistake=0;  
  6.     for(int i=0;i<len1;i++)  
  7.         if(s1[i]!=s2[i])  
  8.         mistake++;  
  9.     if(mistake==1)  
  10.         return true;  
  11.     return false;  
  12. }  


直接操作就可以:

[cpp] view plaincopy
  1. bool replace(char *a,char *b)//判断单词是否能替换掉一个字母而成为词典中的单词  
  2. {  
  3.     int len1=strlen(a);  
  4.     int mistake=0;  
  5.     for(int i=0;i<len1;i++)  
  6.         if(a[i]!=b[i])  
  7.         {  
  8.             mistake++;  
  9.             if(mistake>1)  
  10.             return false;  
  11.         }  
  12.     return true;  
  13. }  

代码:

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdio.h>  
  4. using namespace std;  
  5. const int maxn=10010;  
  6. char dic[maxn][20];  
  7. char wor[60][20];  
  8. int dn,wn;//字典词的个数,需要查询的个数  
  9.   
  10. bool correct(char *s1)//判断该单词是否存在于词典中  
  11. {  
  12.     for(int i=1;i<=dn;i++)  
  13.         if(strcmp(s1,dic[i])==0)  
  14.         return true;  
  15.     return false;  
  16. }  
  17. bool replace(char *a,char *b)//判断单词是否能替换掉一个字母而成为词典中的单词  
  18. {  
  19.     int len1=strlen(a);  
  20.     int mistake=0;  
  21.     for(int i=0;i<len1;i++)  
  22.         if(a[i]!=b[i])  
  23.         {  
  24.             mistake++;  
  25.             if(mistake>1)  
  26.             return false;  
  27.         }  
  28.     return true;  
  29. }  
  30. bool del(char *a,char *b)//判断单词是否能通过删除一个字母而成为词典中的单词  
  31. {  
  32.     int len1=strlen(a);  
  33.     int i=0,j=0;  
  34.     int mistake=0;  
  35.     while(i<len1)  
  36.     {  
  37.         if(a[i]!=b[j])  
  38.         {  
  39.             i++;  
  40.             mistake++;  
  41.             if(mistake>1)  
  42.                 return false;  
  43.         }  
  44.         else  
  45.         {  
  46.             i++;  
  47.             j++;  
  48.         }  
  49.     }  
  50.     return true;  
  51. }  
  52.   
  53. int main()  
  54. {  
  55.     dn=1;  
  56.     wn=1;  
  57.     while(cin>>dic[dn]&&dic[dn][0]!='#') dn++;  
  58.     while(cin>>wor[wn]&&wor[wn][0]!='#') wn++;  
  59.     dn--;wn--;  
  60.     for(int i=1;i<=wn;i++)  
  61.     {  
  62.         if(correct(wor[i]))//首先去词典中查找有没有当前词  
  63.         {  
  64.             cout<<wor[i]<<" is correct"<<endl;  
  65.             continue;  
  66.         }  
  67.         cout<<wor[i]<<": ";  
  68.         for(int j=1;j<=dn;j++)  
  69.         {  
  70.             int len1=strlen(wor[i]);  
  71.             int len2=strlen(dic[j]);  
  72.             if(len1==len2)//长度相等  
  73.             {  
  74.                 if(replace(wor[i],dic[j]))  
  75.                     cout<<dic[j]<<" ";  
  76.             }  
  77.             else if(len1-len2==1)//看单词是否删除一个字母后跟词典中匹配  
  78.             {  
  79.                 if(del(wor[i],dic[j]))  
  80.                     cout<<dic[j]<<" ";  
  81.             }  
  82.             else if(len1-len2==-1)//增加一个字母后跟词典中匹配  
  83.             {  
  84.                 if(del(dic[j],wor[i]))  
  85.                     cout<<dic[j]<<" ";  
  86.             }  
  87.         }  
  88.         cout<<endl;  
  89.     }  
  90.     return 0;  
  91. }  
0 0
原创粉丝点击