-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathCryptogram.java
More file actions
84 lines (73 loc) · 2.83 KB
/
Copy pathCryptogram.java
File metadata and controls
84 lines (73 loc) · 2.83 KB
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
package cryptogram;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Cryptogram {
private static Map<Character, Character> _getAlphabet() {
Map<Character, Character> alphabet = new HashMap<>();
for(char c = 'a'; c <= 'z'; ++c) {
alphabet.put(c, ' ');
}
return alphabet;
}
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
ArrayList<String> data = new ArrayList<>();
Map<Character, Character> alphabet = _getAlphabet();
String templateString = "the quick brown fox jumps over the lazy dog";
boolean success = false; // есть ли решение
Scanner sc = new Scanner(new FileReader("input.txt"));
sc.nextLine(); // первая строка
while (sc.hasNextLine()) {
String line = sc.nextLine();
data.add(line);
if (line.length() == templateString.length() && !success) {
char[] symbols = line.toCharArray();
for (int i = 0; i < symbols.length; i++) {
char templateSymbol = templateString.charAt(i);
if (symbols[i] != ' ') {
alphabet.put(symbols[i], templateSymbol);
}
}
if (alphabet.containsValue(' ')) { // если заполнен не весь алфавит
alphabet = _getAlphabet();
} else {
// новая строка
StringBuilder newLine = new StringBuilder();
// сборка новой строки
for (char symbol : symbols) {
if (symbol == ' ') {
newLine.append(' ');
} else {
newLine.append(alphabet.get(symbol));
}
}
// сравнение новой строки с шаблоном
if (String.valueOf(newLine).equals(templateString)) {
success = true;
}
}
}
}
if (success) {
for (String line : data) {
char[] symbols = line.toCharArray();
for (char symbol: symbols) {
if (symbol != ' ') {
out.print(alphabet.get(symbol));
} else {
out.print(" ");
}
}
out.println();
}
} else {
out.println("No solution");
}
out.flush();
}
}