- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateWord.java
More file actions
Latest commit
101 lines (91 loc) · 2.51 KB
/
Copy pathCreateWord.java
File metadata and controls
101 lines (91 loc) · 2.51 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
85
86
87
88
89
90
91
92
93
94
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.Arrays;
/**
* @백준 1148번
* @author MoonDooo
*/
publicclassCreateWord {
publicstaticvoidmain(String[] args) {
try {
newmakePuzzle();
}catch(IOExceptione) {
e.printStackTrace();
}
}
}
classmakePuzzle{
privatechar[][] dictionary;
privatebyte[] alphabetNum;
privatebyte[] tmpAlphabetNum;
publicmakePuzzle() throwsIOException {
dictionary = newchar[200000][9];
alphabetNum = newbyte[26];
tmpAlphabetNum = newbyte[26];
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
intdictionaryInputIndex =0;
Strings = "";
while(!(s = br.readLine()).equals("-")) {
dictionary[dictionaryInputIndex] = s.toCharArray();
dictionaryInputIndex++;
}
while(!(s = br.readLine()).equals("#")) {
Arrays.fill(alphabetNum, (byte)0);
for(charpuzzleChar : s.toCharArray()) {
alphabetNum[puzzleChar-'A']++;
}
int[] usedAlphabet = newint[26];
for(inti = 0; i < dictionaryInputIndex; i++) {
booleanisCanMakeWord = isCanMakeWord(i);
if(isCanMakeWord) {
for(intj = 0; j<tmpAlphabetNum.length; j++) {
if(tmpAlphabetNum[j]!=alphabetNum[j]) {
usedAlphabet[j]++;
}
}
}
}
intminResult = Integer.MAX_VALUE;
intmaxResult = Integer.MIN_VALUE;
StringBuilderminUsedAlphabet = newStringBuilder();
StringBuildermaxUsedAlphabet = newStringBuilder();
for(inti = 0; i<usedAlphabet.length; i++) {
if(alphabetNum[i]==0) {
continue;
}
if(usedAlphabet[i]>maxResult) {
maxResult = usedAlphabet[i];
maxUsedAlphabet.setLength(0);
maxUsedAlphabet.append((char)('A'+i));
}
elseif(usedAlphabet[i]==maxResult) {
maxUsedAlphabet.append((char)('A'+ i));
}
if(usedAlphabet[i]<minResult) {
minResult = usedAlphabet[i];
minUsedAlphabet.setLength(0);
minUsedAlphabet.append((char)('A'+i));
}
elseif(usedAlphabet[i]==minResult) {
minUsedAlphabet.append((char)('A'+ i));
}
}
StringBuilderresult = newStringBuilder();
result.append(minUsedAlphabet + " ").append(minResult + " ").append(maxUsedAlphabet + " ").append(maxResult);
System.out.println(result);
}
}
publicbooleanisCanMakeWord(inti) {
tmpAlphabetNum = alphabetNum.clone();
for(chardictionaryChar : dictionary[i]) {
if(tmpAlphabetNum[dictionaryChar-'A']<=0) {
returnfalse;
}
else {
tmpAlphabetNum[dictionaryChar-'A']--;
}
}
returntrue;
}
}