Spell checker--POJ 1035

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

 

1、题目类型:字符串、暴力法、trie树。

2、解题思路:(1)记录字符串字典;(2)每输入一个字符串,查找字典中是否存在;(3)如果不存在,依次寻找其是否可以通过字典中字符串替换、添加、删除单个字符获得。

3、注意事项:string.h库函数的使用:trie树的指针的判断,TNode结构体的设计。

4、实现方法:(暴力法)

  
  
1 #include < iostream >
2 #include < string >
3 #include < vector >
4   using namespace std;
5
6 int ok;
7 int ldic, lstr;
8 string current;
9
10 vector < string > dic;
11
12 void Init()
13 {
14 /* 读数据 */
15 while ( 1 )
16 {
17 cin >> current;
18 if (current == " # " ) break ;
19 dic.push_back(current);
20 }
21
22 }
23 /* 替换一个字符 */
24 void replace( string dic)
25 {
26 int count = 0 ;
27 /* 只有一个字符不一样就OK */
28 for ( int i = 0 ; i < dic.length(); i ++ )
29 {
30 if (current[i] != dic[i]) count ++ ;
31 if (count > 1 ) return ;
32 }
33 ok = 1 ;
34 cout << " " + dic;
35 }
36
37 /* 增加一个字符 */
38 void insert( string dic)
39 {
40 string tmp;
41 for ( int i = 0 ; i < current.length(); i ++ )
42 {
43 /* 在不同位置增加字符 */
44 if (current[i] != dic[i])
45 {
46 tmp = dic;
47 tmp.insert(i, 1 , current[i]);
48 if (tmp == current)
49 {
50 ok = 1 ;
51 cout << " " + dic;
52 }
53 return ;
54 }
55 }
56 }
57
58 /* 删除一个字符 */
59 void del( string dic)
60 {
61 string tmp;
62 for ( int i = 0 ; i < dic.length(); i ++ )
63 {
64 /* 删除不同位置的字符 */
65 if (current[i] != dic[i])
66 {
67 tmp = dic;
68 tmp.erase(i, 1 );
69 if (tmp == current)
70 {
71 ok = 1 ;
72 cout << " " + dic;
73 }
74 return ;
75 }
76 }
77 }
78
79 void Seach()
80 {
81 ok = - 1 ;
82 lstr = current.length();
83 /* 判断是否correct */
84 for ( int i = 0 ; i < dic.size(); i ++ )
85 {
86 if (dic[i] == current)
87 {
88 cout << current + " is correct " ;
89 ok = 0 ;
90 break ;
91 }
92 }
93 /* 处理 */
94 if (ok < 0 )
95 {
96 cout << current + ' : ' ;
97 for ( int i = 0 ; i < dic.size(); i ++ )
98 {
99 ldic = dic[i].length();
100 if (lstr == ldic)
101 {
102 replace(dic[i]);
103 }
104 else if (lstr == ldic + 1 )
105 {
106 insert(dic[i]);
107 }
108 else if (lstr == ldic - 1 )
109 {
110 del(dic[i]);
111 }
112 }
113 }
114 }
115 int main()
116 {
117 Init();
118 while ( 1 )
119 {
120 cin >> current;
121 if (current == " # " ) break ;
122 Seach();
123 cout << endl;
124 }
125 return 0 ;
126 }

5、实现方法:(trie树)

  
  
1 #include < iostream >
2 using namespace std;
3
4 char word[ 10001 ][ 27 ],res[ 10001 ][ 27 ];
5 int n,m,len,cnt;
6 bool flag;
7
8 struct TNode
9 {
10 int index,len;
11 TNode * p[ 26 ];
12 TNode()
13 {
14 index = - 1 ;
15 len = 0 ;
16 for ( int i = 0 ; i < 26 ; i ++ )
17 p[i] = NULL;
18 }
19 }root;
20
21 void Create( char * str)
22 {
23 int len1 = strlen(str);
24 TNode * ptr = & root;
25 for ( int i = 0 ; i < len1; i ++ )
26 {
27 if (ptr -> p[str[i] - ' a ' ] == NULL)
28 ptr -> p[str[i] - ' a ' ] = new TNode();
29 ptr = ptr -> p[str[i] - ' a ' ];
30 }
31 ptr -> index = cnt ++ ;
32 ptr -> len = len1;
33 }
34
35 int search( char * str)
36 {
37 int len1 = strlen(str);
38 TNode * ptr = & root;
39 for ( int i = 0 ; i < len1; i ++ )
40 {
41 if (ptr -> p[str[i] - ' a ' ] == NULL)
42 return - 1 ;
43 ptr = ptr -> p[str[i] - ' a ' ];
44 }
45 if (ptr -> len == len1)
46 return ptr -> index;
47 else return - 1 ;
48 }
49
50 void ADD( char * str)
51 {
52 int pos,len1 = strlen(str),i,k;
53 char z,temp[ 30 ];
54 for (i = 0 ;i <= len1;i ++ )
55 {
56 for (z = ' a ' ;z <= ' z ' ;z ++ )
57 {
58 for (k = 0 ;k < i;k ++ )
59 temp[k] = str[k];
60 temp[i] = z;
61 for (k = i + 1 ;k <= len1;k ++ )
62 temp[k] = str[k - 1 ];
63 temp[len1 + 1 ] = ' /0 ' ;
64 pos = search(temp);
65 if (pos == - 1 )
66 continue ;
67 strcpy(res[pos],temp);
68 flag = 1 ;
69 }
70 }
71 }
72 void Delete( char * str)
73 {
74 char temp[ 30 ];
75 int pos,i,j,k,len1 = strlen(str);
76 for (i = 0 ;i < len1;i ++ )
77 {
78 k = 0 ;
79 for (j = 0 ;j < len1;j ++ )
80 {
81 if (j == i)
82 continue ;
83 temp[k ++ ] = str[j];
84 }
85 temp[k] = ' /0 ' ;
86 pos = search(temp);
87 if (pos == - 1 )
88 continue ;
89 strcpy(res[pos],temp);
90 flag = 1 ;
91 }
92 }
93 void Change( char * str)
94 {
95 int len1 = strlen(str),i,j,pos;
96 char z, temp[ 30 ];
97 for (j = 0 ;j < len1;j ++ )
98 temp[j] = str[j];
99 temp[len1] = ' /0 ' ;
100 for (i = 0 ;i < len1;i ++ )
101 {
102 for (z = ' a ' ;z <= ' z ' ;z ++ )
103 {
104 if (z == str[i])
105 continue ;
106 temp[i] = z;
107 pos = search(temp);
108 if (pos == - 1 )
109 continue ;
110 strcpy(res[pos],temp);
111 flag = 1 ;
112 }
113 temp[i] = str[i];
114 }
115 }
116
117 int main()
118 {
119 int i;
120 for (i = 0 ; ; i ++ )
121 {
122 scanf( " %s " ,word[i]);
123 if (word[i][ 0 ] == ' # ' )
124 break ;
125 Create(word[i]);
126 }
127 n = i;
128 char query[ 30 ];
129 for (i = 0 ; ; i ++ )
130 {
131 flag = false ;
132 scanf( " %s " ,query);
133 memset(res, 0 , sizeof (res));
134 if (query[ 0 ] == ' # ' )
135 break ;
136 int kk = search(query);
137 if (kk != - 1 )
138 {
139 cout << query << " is correct " << endl;
140 continue ;
141 }
142 ADD(query);
143 Delete(query);
144 Change(query);
145 if (flag)
146 {
147 cout << query << " : " ;
148 for ( int k = 0 ; k < 10001 ;k ++ )
149 if (res[k][ 0 ] != 0 )
150 cout << " " << res[k];
151 cout << endl;
152 }
153 else
154 cout << query << " : " << endl;
155 }
156 return 0 ;
157 }

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值