智能T9英文輸入法
Time Limit:1000MSMemory Limit:30000KB
Total Submit:235Accepted:57
Description
某款新型手機為了方便用戶,希望開發(fā)一種新的英文輸入法.要求在輸入英文的時候輸入法不但能夠做到自動聯(lián)想,還能進行自動
糾錯.譬如用戶希望輸入hello這個單詞,他應該輸入43556,但是他不小心輸入了46556.輸入法發(fā)現(xiàn)詞庫中找不到任何匹配的單詞,
于是嘗試把6糾正為3,這便是糾錯功能.現(xiàn)在需要你來開發(fā)這個輸入法的核心部分.
給出詞庫和用戶的輸入,請你依次給出適合的單詞.
2 A B C
3 D E F
4 G H I
5 J K L
6 M N O
7 P Q R S
8 T U V
9 W X Y Z
注意:1和0沒有對應的字母,但是1和0也有可能出現(xiàn).
Input
該題含有多組測試數(shù)據(jù)。
每組數(shù)據(jù)第一行是一個整數(shù)n(1<=n<=100),表示詞庫中的單詞個數(shù).
接下來n行每行是一個詞庫中的單詞.單詞只包含大寫字母,長度不會超過10.不會出現(xiàn)兩個相同的單詞.
最后一行是一個數(shù)字串表示用戶的輸入.輸入的長度不會超過10.
Output
對于每組測試數(shù)據(jù)的輸出,包含四個部分.
首先輸出完全符合輸入的單詞.
然后是根據(jù)聯(lián)想得到的單詞,即前綴部分完全符合輸入的單詞.
接下來輸出糾正一個按鍵之后完全符合輸入的單詞.
然后是糾正一個按鍵之后聯(lián)想得到的單詞.
每部分如果有多個匹配,則按字典順序輸出.
保證不會出現(xiàn)無解情況.
Sample Input
6
BVUJMEE
MUTKOE
BTVLOE
ATVKEI
EVTJNJHF
OVVLMFAABC
288563
Sample Output
BTVLOE
BVUJMEE
MUTKOE
OVVLMFAABC
Source
浙江省2004組隊賽第二試(From TOJ)
暴力,深度優(yōu)先搜索,根據(jù)輸入數(shù)字,窮舉所有可能的字母組合,判斷每種字母組合是否符合要求。
睡覺前心血來潮想寫寫這個題目,結(jié)果寫到現(xiàn)在,明天補覺。
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
5
#define N 109
6
#define L 13
7
#define K 13
8
#define E ('Z'+1)
9
#define P 13
10
11
int n;
12
char word[ N ][ L ];
13
char key[ K ] = "**ADGJMPTW*";
14
15
int m;
16
char press[ P ];
17
18
/**//* 二維字符數(shù)組 */
19
int cmpA( const void *a, const void *b )
{
20
return strcmp( ((const char*)a), ((const char*)b) );
21
}
22
/**//* 一維字符指針 */
23
int cmpP( const void *a, const void *b )
{
24
return strcmp( (*((const char**)a)), (*((const char**)b)) );
25
}
26
27
int topAns, topPre, topAns1, topPre1;
28
char *ans[ N ], *pre[ N ], *ans1[ N ], *pre1[ N ];
29
30
char tmp[ P ];
31
int mod1;
32
33
void update()
{
34
int i, j;
35
for ( i = 0; i < n; ++i )
{
36
for ( j = 0; (tmp[j])&&(word[i][j])&&(tmp[j]==word[i][j]); ++j )
37
;
38
if ( 0 == tmp[ j ] )
{
39
if ( 0 == word[ i ][ j ] )
{
40
if ( mod1 )
{
41
ans1[ topAns1++ ] = word[ i ];
42
}
43
else
{
44
ans[ topAns++ ] = word[ i ];
45
}
46
}
47
else
{
48
if ( mod1 )
{
49
pre1[ topPre1++ ] = word[ i ];
50
}
51
else
{
52
pre[ topPre++ ] = word[ i ];
53
}
54
}
55
}
56
}
57
}
58
59
void solve( int i )
{
60
char j, k;
61
if ( i >= m )
{
62
tmp[ i ] = 0;
63
update();
64
return;
65
}
66
for ( j = key[ press[ i ] ]; j < key[ press[ i ] + 1 ]; ++j )
{
67
tmp[ i ] = j;
68
solve( i+1 );
69
}
70
if ( 0 == mod1 )
{
71
mod1 = 1;
72
for ( k = 2; k < 10; ++k )
{
73
if ( k == press[ i ] )
{
74
continue;
75
}
76
for ( j = key[ k ]; j < key[ k + 1 ]; ++j )
{
77
tmp[ i ] = j;
78
solve( i+1 );
79
}
80
}
81
mod1 = 0;
82
}
83
}
84
85
void out( int top, char * stack[] )
{
86
int i;
87
if ( top > 0 )
{
88
qsort( stack, top, sizeof(char*), cmpP );
89
for ( i = 0; i < top; ++i )
{
90
puts( stack[ i ] );
91
}
92
}
93
}
94
95
void output()
{
96
out( topAns, ans );
97
out( topPre, pre );
98
out( topAns1, ans1 );
99
out( topPre1, pre1 );
100
}
101
102
int main()
{
103
int i;
104
key[ 0 ] = key[ 1 ] = key[ 10 ] = E;
105
while ( 1 == scanf( "%d", &n ) )
{
106
for ( i = 0; i < n; ++i )
{
107
scanf( "%s", word[ i ] );
108
}
109
/**//* qsort( word, n, sizeof(word[0]), cmpA ); */
110
scanf( "%s", press );
111
m = strlen( press );
112
for ( i = 0; i < m; ++i )
{
113
press[ i ] -= '0';
114
}
115
topAns = topPre = topAns1 = topPre1 = 0;
116
mod1 = 0;
117
solve( 0 );
118
output();
119
}
120
return 0;
121
}
122
09年10月通過的代碼,用了 Trie ,懶得分析了。
1
#include <iostream>
2
#include <cstring>
3
using namespace std;
4
5
char * letter[300];
6
7
#define TW ('Z'+1)
8
#define TWB 'A'
9
#define EOW 0
10
struct WordNode
11

{
12
WordNode()
{
13
bExist = 0;
14
memset( ch, 0, sizeof(ch) );
15
}
16
int bExist;
17
WordNode * ch[TW];
18
};
19
20
WordNode * root = 0;
21
22
#define WL 20
23
#define NW 100000
24
char fw[NW][WL], wz[WL];
25
int nw, nz;
26
27
void WordInsert( WordNode * & roo, char * p )
{
28
if( roo == 0 )
{
29
roo = new WordNode;
30
}
31
if( *p != EOW )
{
32
WordInsert( roo->ch[*p], p+1 );
33
}
34
else
{
35
roo->bExist = 1;
36
}
37
return;
38
}
39
40
void WordClear( WordNode * & roo )
{
41
char i;
42
if( roo == 0 )
{
43
return;
44
}
45
for( i=TWB; i<TW; ++i )
{
46
WordClear( roo->ch[i] );
47
}
48
delete roo;
49
roo = 0;
50
return;
51
}
52
53
void FindWord( WordNode * roo, char * p )
{
54
char *pl;
55
if( roo == 0 )
{
56
return;
57
}
58
if( *p == EOW )
{
59
if( roo->bExist )
{
60
strcpy( fw[nw], wz );
61
++nw;
62
}
63
return;
64
}
65
if( (*p=='0') || (*p=='1') )
{
66
return;
67
}
68
69
++nz;
70
for( pl=letter[*p]; *pl!=EOW; ++pl )
{
71
wz[nz-1] = *pl;
72
FindWord( roo->ch[*pl], p+1 );
73
}
74
wz[--nz] = 0;
75
return;
76
}
77
78
void FindAssociateWord( WordNode * roo, char * p )
{
79
char *pl, i;
80
if( roo == 0 )
{
81
return;
82
}
83
84
if( p == 0 )
{
85
if( roo->bExist )
{
86
strcpy( fw[nw], wz );
87
++nw;
88
}
89
++nz;
90
for( i=TWB; i<TW; ++i )
{
91
wz[nz-1] = i;
92
FindAssociateWord( roo->ch[i], 0 );
93
}
94
wz[--nz] = 0;
95
return;
96
}
97
98
if( *p == EOW )
{
99
++nz;
100
for( i=TWB; i<TW; ++i )
{
101
wz[nz-1] = i;
102
FindAssociateWord( roo->ch[i], 0 );
103
}
104
wz[--nz] = 0;
105
return;
106
}
107
108
if( (*p=='0') || (*p=='1') )
{
109
return;
110
}
111
112
++nz;
113
for( pl=letter[*p]; *pl!=EOW; ++pl )
{
114
wz[nz-1] = *pl;
115
FindAssociateWord( roo->ch[*pl], p+1 );
116
}
117
wz[--nz] = 0;
118
return;
119
}
120
121
void FindOutput( void )
{
122
char * w[NW], *temp;
123
int i, j, k;
124
for( i=0; i<nw; ++i )
{
125
w[i] = fw[i];
126
}
127
for( i=0; i<nw; ++i )
{
128
k = i;
129
for( j=i+1; j<nw; ++j )
{
130
if( strcmp( w[k], w[j] ) > 0 )
{
131
k = j;
132
}
133
}
134
if( k != i )
{
135
temp = w[k]; w[k] = w[i]; w[i] = temp;
136
}
137
printf( "%s\n", w[i] );
138
}
139
nw = 0;
140
return;
141
}
142
143
int main()
{
144
int n;
145
char tc, s[WL], *pc, i;
146
147
memset( letter, 0, sizeof(letter) );
148
letter['2'] = "ABC";
149
letter['3'] = "DEF";
150
letter['4'] = "GHI";
151
letter['5'] = "JKL";
152
letter['6'] = "MNO";
153
letter['7'] = "PQRS";
154
letter['8'] = "TUV";
155
letter['9'] = "WXYZ";
156
157
while( EOF != scanf( "%d", &n ) )
{
158
WordClear( root );
159
160
while( n-- )
{
161
scanf( "%s", s );
162
WordInsert( root, s );
163
}
164
scanf( "%s", s );
165
166
nw = nz = 0;
167
FindWord( root, s );
168
FindOutput();
169
170
nw = nz = 0;
171
FindAssociateWord( root, s );
172
FindOutput();
173
174
nw = nz = 0;
175
for( pc=s; *pc!=EOW; ++pc )
{
176
for( i='2'; i<='9'; ++i )
{
177
if( i != *pc )
{
178
tc = *pc;
179
*pc = i;
180
FindWord( root, s );
181
*pc = tc;
182
}
183
}
184
}
185
FindOutput();
186
187
nw = nz = 0;
188
for( pc=s; *pc!=EOW; ++pc )
{
189
for( i='2'; i<='9'; ++i )
{
190
if( i != *pc )
{
191
tc = *pc;
192
*pc = i;
193
FindAssociateWord( root, s );
194
*pc = tc;
195
}
196
}
197
}
198
FindOutput();
199
}
200
return 0;
201
}
202