-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.java
34 lines (30 loc) · 974 Bytes
/
D.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
package com.company;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class D {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("lzw.in"));
FileWriter fileWriter = new FileWriter(new File("lzw.out"));
String s = scanner.nextLine();
ArrayList<String> dict = new ArrayList<>();
for (int i = 0; i < 26; i++) {
dict.add((char) (i + 97) + "");
}
String t = "";
for (int i = 0; i < s.length(); i++) {
String c = s.charAt(i) + "";
if (dict.indexOf(t + c) != -1) {
t = t + c;
} else {
fileWriter.write(dict.indexOf(t) + " ");
dict.add(t + c);
t = c;
}
}
fileWriter.write(dict.indexOf(t) + " ");
fileWriter.flush();
}
}