[POJ1035 Spell checker]

[题目来源]:Northeastern Europe 1998

[关键字]:字符串处理

[题目大意]:给出一个字典,然后查询一些字符是否能字典中的匹配。匹配是指:1、相等;2、少一个其余相等;3、多一个其余相等。

//=====================================================================================================

[分析]:只要根据要求一一测试,将正在查的单词与字典中的每一个比对,如果长度相等则判断是否一样,如果多一个则枚举删掉该词的一位再判断是否一样,如果少一个则枚举删掉字典中那个词的一位再判断。

[代码]:

View Code
  1 {
2 PROB:POJ1035
3 DATE:2011\10\16
4 }
5 type
6 rec = record
7 s: string;
8 num: longint;
9 dat: array[0..10000] of string;
10 end;
11 var
12 tot: longint;
13 w: array[0..10010] of string;
14 ans: array[0..60] of rec;
15
16 function ind(s: string):boolean;
17 var
18 i: longint;
19 begin
20 for i := 1 to tot do
21 if s = w[i] then exit(true);
22 exit(false);
23 end;
24
25 procedure instead(s: string; t: longint);
26 var
27 i, j, dif: longint;
28 s2: string;
29 begin
30 for i := 1 to tot do
31 begin
32 if length(s)-length(w[i]) = 1 then
33 for j := 1 to length(s) do
34 begin
35 s2 := s;
36 delete(s2,j,1);
37 if s2 = w[i] then
38 begin
39 write('',w[i]);
40 break;
41 end;
42 end;
43 if length(s) = length(w[i]) then
44 begin
45 dif := 0;
46 for j := 1 to length(s) do
47 begin
48 if s[j] <> w[i][j] then inc(dif);
49 if dif = 2 then break;
50 end;
51 if dif = 1 then write('',w[i]);
52 end;
53 if length(s)-length(w[i]) = -1 then
54 for j := 1 to length(w[i]) do
55 begin
56 s2 := w[i];
57 delete(s2,j,1);
58 if s2 = s then
59 begin
60 write('',w[i]);
61 break;
62 end;
63 end;
64 end;
65 end;
66
67 procedure init;
68 var
69 s: string;
70 i, j, t: longint;
71 temp: rec;
72 begin
73 readln(s);
74 while s[1] <> '#' do
75 begin
76 inc(tot);
77 w[tot] := s;
78 readln(s);
79 end;
80 //========================================================================
81 readln(s);
82 t := 0;
83 while s[1] <> '#' do
84 begin
85 if ind(s) then
86 begin writeln(s,' is correct'); readln(s); continue; end;
87 write(s,':');
88 instead(s,t);
89 writeln;
90 readln(s);
91 end;
92 //=========================================================================
93 end;
94
95 begin
96 assign(input,'1.in');reset(input);
97 assign(output,'1.out');rewrite(output);
98 init;
99 close(input);
100 close(output);
101 end.



你可能感兴趣的:(check)