-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCensorship.java
95 lines (72 loc) · 2.4 KB
/
Censorship.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
import java.util.*;
import java.io.*;
public class Censorship {
private static ArrayList<String> words;
public static void main(String... args) throws FileNotFoundException {
words = new ArrayList<String>();
Scanner file = new Scanner(new File("judgesdata\\censorship.dat"));
int size = file.nextInt(); clearFollowingLine(file);
String[] msgs = new String[size];
for(int i = 0; i < size; i++) {
// Look through words to check what needs to be censored, and censor
// only wordsCopy. Map the two arrays.
String[] wordsToCensor = file.nextLine().split(" ");
String[] words = file.nextLine().split(" ");
String[] wordsCopy = copyArray(words);
words = lowercaseAll(words);
wordsToCensor = lowercaseAll(wordsToCensor);
int sizeOfWords = words.length-1;
for(int j = 0; j < sizeOfWords; j++) {
// if `word` is a word to be censored:
if(contains(wordsToCensor, words[j])) {
// Overwrite the word with all *'s. "china" -> "*****"
wordsCopy[j] = genStarMsg(words[j].toCharArray().length);
}
if(containsRosie(words[j], words[j+1])) {
wordsCopy[j] = genStarMsg(words[j].toCharArray().length);
wordsCopy[j+1] = genStarMsg(words[j+1].toCharArray().length);
}
}
if(contains(wordsToCensor, words[sizeOfWords]))
wordsCopy[sizeOfWords] = genStarMsg(words[sizeOfWords].toCharArray().length);
msgs[i] = join(wordsCopy);
}
for(String s: msgs)
System.out.println(s);
}
public static String[] copyArray(String[] s) {
String[] c = new String[s.length];
for(int i = 0; i < s.length; i++)
c[i] = s[i];
return c;
}
public static void clearFollowingLine(Scanner scan) {
scan.nextLine();
}
public static String[] lowercaseAll(String[] msg) {
String[] s = new String[msg.length];
for(int i = 0; i < msg.length; i++) {
s[i] = msg[i].toLowerCase();
}
return s;
}
public static String join(String[] list) {
String msg = "";
for(int i = 0; i < list.length-1; i++)
msg += list[i] + " ";
return msg + list[list.length-1];
}
public static String genStarMsg(int size) {
String msg = "";
for(int i = 0; i < size; i++) msg += "*";
return msg;
}
public static boolean contains(String[] list, String msg) {
for(String s: list)
if(msg.equals(s)) return true;
return false;
}
public static boolean containsRosie(String msg, String msg2) {
return msg.equals("rosie") && msg2.equals("o'donnell");
}
}