-
Notifications
You must be signed in to change notification settings - Fork 7
/
1035 Spell checker.java
153 lines (144 loc) · 3.62 KB
/
1035 Spell checker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.io.*;
import java.util.*;
class Item implements Comparable< Item>
{
int index;
String str;
Item(int i,String s)
{
index = i;
str = s;
}
public int compareTo(Item it)
{
if(index>it.index)
return 1;
else if(index< it.index)
return -1;
else
return 0;
}
}
public class Main
{
private static List< Item> dicts = new ArrayList< Item>();
public static void main(String[] args) throws Exception
{
readFile();
}
private static void readFile() throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
String temp = null;
int iCount = 1;
while(!(temp = br.readLine()).equals("#"))
dicts.add(new Item(iCount++,temp));
int flag = 0;
while(!(temp = br.readLine()).equals("#"))
{
if(flag != 0)
System.out.println();
process(temp);
flag++;
}
}
private static void process(String str)
{
List< Item> result = new ArrayList< Item>();
List< Item> temp = null;
if(isHave(str))
{
System.out.print(str + " is correct");
return;
}
temp = getArrayList(str.length() + 1);
for(Item it : temp)
if(judge(str,it.str))
result.add(it);
temp = getArrayList(str.length() - 1);
for(Item it : temp)
if(judge(str,it.str))
result.add(it);
temp = getArrayList(str.length());
for(Item it : temp)
if(judge1(str,it.str))
result.add(it);
Collections.sort(result);
System.out.print(str + ":");
if(result.size() != 0)
System.out.print(" ");
int flag = 0;
for(Item it : result)
{
if(flag!=0)
System.out.print(" ");
System.out.print(it.str);
flag++;
}
}
private static boolean judge1(String src,String dest)
{
int i = 0;
int count = 0;
while(i< src.length())
{
if(src.charAt(i)!=dest.charAt(i))
{
count++;
}
if(count>1)
return false;
i++;
}
return true;
}
private static boolean judge(String src,String dest)
{
String min = dest;
String max = src;
if(src.length() < dest.length())
{
min = src;
max = dest;
}
int count = 0;
int i = 0;
int j = 0;
while(i < max.length()&&j < min.length())
{
if(max.charAt(i) != min.charAt(j))
{
count++;
i++;
}
else
{
i++;
j++;
}
if(count > 1)
return false;
}
return true;
}
private static boolean isHave(String str)
{
for(Item it : dicts)
{
if(it.str.equals(str))
return true;
}
return false;
}
private static ArrayList< Item> getArrayList(int n)
{
ArrayList< Item> result = new ArrayList< Item>();
for(Item it : dicts)
{
if(it.str.length()==n)
result.add(it);
}
return result;
}
}