-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathCompressionBinarySequences.java
More file actions
37 lines (35 loc) · 1.33 KB
/
Copy pathCompressionBinarySequences.java
File metadata and controls
37 lines (35 loc) · 1.33 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
package compression_binary_sequences;
import java.util.*;
import java.io.*;
public class CompressionBinarySequences {
private static ArrayList<String> symbols = new ArrayList<>();
private static void getData() throws IOException{
FileReader file = new FileReader("input.txt");
Scanner sc = new Scanner(file);
String str = sc.nextLine();
char[] ch = str.toCharArray();
for (char aCh : ch) {
String st = String.valueOf(aCh);
if (st.equals("1") || st.equals("0")) {
symbols.add(st);
}
}
}
public static void main(String[] argv) throws IOException{
char[] abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
getData();
int k = 0; // Счетчик количества нулей перед 1
StringBuilder code = new StringBuilder(); // Итоговая строка
for (String symbol : symbols) {
if (symbol.equals("1")) {
code.append(String.valueOf(abc[k])); // Добавить к строке букву
k = 0;
} else {
k++;
}
}
PrintWriter pw = new PrintWriter(new File("output.txt"));
pw.print(code);
pw.close();
}
}