poj 1035--Spell checker

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

给出一个字典表之后,没输入一个单词进行比对,看是否存在相同的,或是可以通过删除,添加,更改一个字符
使其变成表中的一个数据的。。输出可能的情况。直接找就行。由题意可知,只要寻找长度差|x|<=1的即可

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 char dic[10001][20];
 7 int cnt;
 8 int del(char a[],char b[])
 9 {
10     int i,j;
11     for(i=0;a[i];i++)
12         if(a[i]!=b[i])
13             break;
14     if(!strcmp(a+i+1,b+i)) return 1;
15     return 0;
16 }
17 int rep(char a[],char b[])
18 {
19     int i;
20     for(i=0;a[i];i++)
21         if(a[i]!=b[i])
22             break;
23     if(!strcmp(a+i+1,b+i+1)) return 1;
24     return 0;
25 }
26 int add(char a[],char b[])
27 {
28     int i;
29     for(i=0;b[i];i++)
30         if(a[i]!=b[i])
31             break;
32     if(!strcmp(a+i,b+i+1)) return 1;
33     return 0;
34 }
35 int main()
36 {
37     int i,j,flag,k,p,q;
38     char s[20];
39     cnt=0;
40     while(scanf("%s",dic[cnt]))
41     {
42         if(!strcmp(dic[cnt],"#"))
43             break;
44         cnt++;
45     }
46     while(scanf("%s",s))
47     {
48         if(s[0]=='#') break;
49         flag=1;
50         for(i=0;i<cnt&&flag;i++)
51         {
52             if(!strcmp(dic[i],s))
53             {
54                 printf("%s is correct",s);
55                 flag=0;
56                 break;
57             }
58         }
59         if(flag) printf("%s:",s);
60         for(i=0;i<cnt&&flag;i++)
61         {
62             if(strlen(s)-strlen(dic[i])==1)
63                 if(del(s,dic[i])) 
64                     printf(" %s",dic[i]);
65             if(strlen(s)-strlen(dic[i])==0)
66                     if(rep(s,dic[i]))
67                         printf(" %s",dic[i]);
68             if(strlen(s)-strlen(dic[i])==-1)
69                     if(add(s,dic[i]))
70                         printf(" %s",dic[i]);
71         }
72         printf("\n");
73     }
74     return 0;
75 }

 

posted @ 2013-04-08 20:27  Tamara.c  阅读(196)  评论(0编辑  收藏  举报