-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.java
60 lines (53 loc) · 1.64 KB
/
solution.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
import java.io.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Main {
public static int mod(int a, int b) {
return (a + b) % b;
}
public static String find(String str, Set<String> mp) {
for (int i = 0; i < 27; i++) {
StringBuilder s = new StringBuilder();
for (var ch : str.toCharArray()) {
s.append((char) (mod((int) ch + i - (int) 'a', 26) + (int)'a'));
}
if (mp.contains(s.toString())) {
return s.toString();
}
}
return "";
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String buffer = reader.readLine();
Set<String> mp = new HashSet<>();
StringBuilder currword = new StringBuilder();
int c = 0;
for (int i = 0; i < buffer.length(); i++) {
if (buffer.charAt(i) == ' ' || buffer.charAt(i) == '\n') {
mp.add(currword.toString());
currword = new StringBuilder();
} else {
currword.append(buffer.charAt(i));
}
}
mp.add(currword.toString());
int n = Integer.parseInt(reader.readLine());
for (int i = 0; i < n; i++) {
String str = reader.readLine();
writer.write(find(str.substring(0, str.length()), mp) + "\n");
}
reader.close();
writer.close();
}
}
/*
a
3
b
v
c
*/