-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSACO8.java
224 lines (194 loc) · 6.25 KB
/
USACO8.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
USER: achyuta2
PROB: namenum
LANG: JAVA
*/
import java.io.*;
import java.lang.*;
import java.util.Arrays;
import java.util.Objects;
public class namenum {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("namenum.in"));
BufferedReader f = new BufferedReader(new FileReader("dict.txt"));
PrintWriter out = new PrintWriter(new FileWriter("namenum.out"));
String [] dict = new String[4617];
String num =br.readLine();
int d = num.length();
int p = (int)Math.pow(3 , d);
for(int n=0; n<4617; n++){
dict[n]=f.readLine();
}
StringBuilder[] g = new StringBuilder[x];
for(int z=0;z<g.length;z++){
g[z]=new StringBuilder();
}
int s = 3;
for(int u=0; u<d;u++){
char q =num.charAt(u);
if(q=='2'){
int w = 0;
for (int y = 0; y<(s/3);y++){
for (int e=0; e<(p/s);e++){
g[w].append('A');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('B');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('C');
w++;
}
}
}
if(q=='3'){
int w = 0;
for (int y = 0; y<(s/3);y++){
for (int e=0; e<(p/s);e++){
g[w].append('D');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('E');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('F');
w++;
}
}
}
if(q=='4'){
int w = 0;
for (int y = 0; y<(s/3);y++){
for (int e=0; e<(p/s);e++){
g[w].append('G');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('H');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('I');
w++;
}
}
}
if(q=='5'){
int w=0;
for (int y = 0; y<(s/3);y++){
for (int e=0; e<(p/s);e++){
g[w].append('J');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('K');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('L');
w++;
}
}
}
if(q=='6'){
int w = 0;
for (int y = 0; y<(s/3);y++){
for (int e=0; e<(p/s);e++){
g[w].append('M');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('N');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('O');
w++;
}
}
}
if(q=='7'){
int w =0;
for (int y = 0; y<(s/3);y++){
for (int e=0; e<(p/s);e++){
g[w].append('P');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('R');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('S');
w++;
}
}
}
if(q=='8'){
int w=0;
for (int y = 0; y<(s/3);y++){
for (int e=0; e<(p/s);e++){
g[w].append('T');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('U');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('V');
w++;
}
}
}
if(q=='9'){
int w=0;
for (int y = 0; y<(s/3);y++){
for (int e=0; e<(p/s);e++){
g[w].append('W');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('Y');
w++;
}
for (int e=0; e<(p/s);e++){
g[w].append('Z');
w++;
}
}
}
s = s * 3;
}
String [] c=new String[x];
for (int i=0;i<g.length;i++) {
c[i]=g[i].toString();
}
String [] validNames= new String[x];
for(int y=0; y<c.length;y++){
if(isValidName(c[y],dict)){
validNames[y]=c[y];
}
}
validNames=Arrays.stream(validNames).filter(Objects::nonNull).toArray(String[]::new);
for(String n :validNames){
out.println(n);
}
if(validNames.length==0){
out.println("NONE");
}
out.close();
}
public static boolean isValidName(String test, String[] dictionary){
int y = Arrays.binarySearch(dictionary,test);
if(y==-1){
return false;}
else{
return true;
}
}
}