Spell checker 字典树&&普通查找(加计数)
Problem 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.
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.
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
***************************************************************************************************************************
字典树
***************************************************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 using namespace std; 7 char str1[10101][55],str2[55]; 8 int i,j; 9 class node 10 { 11 public: 12 node *p[26]; 13 int it; 14 bool de; 15 node() 16 { 17 for(it=0;it<26;it++) 18 p[it]=NULL; 19 de=false;; 20 } 21 }; 22 node *root; 23 void get_num(char*s) 24 { 25 node*r=root; 26 int len1=strlen(s); 27 int it; 28 for(it=0;it<len1;it++) 29 { 30 if(r->p[s[it]-'a']==NULL) 31 { 32 r->p[s[it]-'a']=new node(); 33 } 34 r=r->p[s[it]-'a']; 35 } 36 r->de=true; 37 } 38 bool find(char *s) 39 { 40 int len2=strlen(s); 41 int it; 42 node *r=root; 43 for(it=0;it<len2;it++) 44 { 45 if(r->p[s[it]-'a']==NULL) 46 return false; 47 r=r->p[s[it]-'a']; 48 } 49 if(r->de) 50 return true; 51 return false; 52 } 53 bool solve(char s1[],char s2[]) 54 { 55 int len1=strlen(s1); 56 int len2=strlen(s2); 57 int ncount=0; 58 if(len1!=len2) 59 { 60 int it=0,jt=0; 61 while(it<len1&&jt<len2) 62 { 63 if(s1[it]!=s2[jt]) 64 { 65 ncount++; 66 if(len1>len2) 67 { 68 it++; 69 } 70 else 71 jt++; 72 } 73 else 74 { 75 it++; 76 jt++; 77 } 78 } 79 if(ncount>1) 80 return false; 81 else 82 return true; 83 } 84 else 85 { 86 int it=0; 87 while(it<len1) 88 { 89 if(s1[it]!=s2[it]) 90 ncount++; 91 it++; 92 } 93 if(ncount==1) 94 return true; 95 else 96 return false; 97 } 98 } 99 int main() 100 { 101 int st=0; 102 root=new node(); 103 while(scanf("%s",str1[st])&&str1[st][0]!='#') 104 { 105 get_num(str1[st]); 106 st++; 107 } 108 while(scanf("%s",str2)&&str2[0]!='#') 109 { 110 if(find(str2))//判断是否有 111 printf("%s is correct\n",str2); 112 else 113 { 114 printf("%s:",str2); 115 for(i=0;i<st;i++) 116 { 117 int temp=strlen(str1[i])-strlen(str2); 118 if(temp<=1&&temp>=-1) 119 if(solve(str1[i],str2))//判断是否有一个不同或可替换 120 printf(" %s",str1[i]); 121 } 122 printf("\n"); 123 } 124 } 125 return 0; 126 127 }