Spell checker--POJ 1035

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 }

 

你可能感兴趣的:(check)