-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
319 lines (295 loc) · 8.37 KB
/
index.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// const readline = require("readline");
// process.stdin.resume();
// process.stdin.setEncoding("utf8");
// const reader = readline.createInterface({
// input: process.stdin,
// output: process.stdout,
// });
// function zip(str) {
// const temp = [];
// let wide = 1;
// while (str.length / 2 >= wide) {
// let store = "";
// for (let i = 0; i < str.length; i++) {
// const piece = str.slice(i, i + wide);
// // const data = {
// // w: piece,
// // c: 1,
// // };
// // console.log(piece);
// if (str.slice(i + wide).startsWith(piece)) {
// store[store.length - 1] = Number(store[store.length - 1]) + 1;
// i += wide;
// } else {
// continue;
// }
// store += piece + "1";
// // console.log(store);
// }
// temp.push(store);
// wide++;
// }
// console.log(temp);
// const words = temp.map((t) => t);
// console.log(words);
// const len = words.map((_) => _.length);
// const min = Math.min(...len);
// const index = len.findIndex((_) => _ === min);
// return words[index];
// }
// let dump = [];
// console.log("입력하세요.");
// reader.on("line", (line) => {
// if (line === "x") {
// reader.close();
// return;
// }
// dump.push(line);
// console.log("current data:", dump);
// console.log("추가로 입력하세요.");
// });
// reader.on("close", () => {
// // console.log("입력 결과", dump);
// console.log(zip(dump.join("")));
// });
// // console.log(
// // zip(
// // )
// // );
/* testasasbobbob 에서 오류가 있음 */
/*
test2as2bob - 11 최소
testasasbobbob - 14 최대
하지만 아래 솔류션은 test2asbobbob - 13을 최소라고 함
심지어 testasas2bob - 12 는 결과로 나오지도 않음
*/
// function solution(s) {
// const lengths = [];
// let length = 1;
// while (length <= s.length) {
// let result = compress(length, s);
// console.log("result", result);
// lengths.push(result.length);
// length += 1;
// }
// return Math.min(...lengths);
// }
// function compress(length, s) {
// let result = "";
// let current = s.slice(0, length);
// console.log("current", current);
// let count = 1;
// for (let i = length; i < s.length; i += length) {
// let snippet = s.slice(i, i + length);
// if (snippet === current) {
// count += 1;
// } else {
// if (count > 1) {
// result += String(count);
// }
// count = 1;
// result += current;
// current = snippet;
// }
// }
// console.log("compress result", s, length, current, result);
// if (count > 1) {
// result += String(count);
// }
// result += current;
// return result;
// }
/* 새로운 솔루션 제작 */
function solution(s) {}
function duplicateRemove(s) {
let words = s;
for (let i = 0; i < s.length; i++) {
if (words.replace(new RegExp(`[${words.at(i)}]+`, "g"), "") === "") {
words = words.replace(
new RegExp(`[${words.at(i)}]+`, "g"),
($1) => $1[0]
);
} else {
words = words;
}
}
return words;
}
function duplicateItemRemove(arr) {
let temp = {};
for (let item of arr) {
temp[item] = null;
}
return Object.keys(temp);
}
function compress(s) {
// window.parent.completeFiltered = [];
if (s.trim() === "") {
this?.classList?.add?.("error");
setTimeout(() => {
this?.removeAttribute?.("class");
}, 300);
return;
}
this?.classList?.add?.("clicked");
setTimeout(() => {
this?.removeAttribute?.("class");
}, 150);
let count = 0;
let words = s;
for (let word of findDuplicateWord(s).reverse()) {
const counted = words.replace(new RegExp(word, "g"), ($1, i) => {
// console.log("slice", words.slice(i, word.length + i), i);
// console.log(
// "next",
// words.slice(word.length + i, i + word.length * 2),
// word.length + i
// );
if (
i === 0 ||
i + word.length === words.indexOf(word, i + 1) ||
words.slice(i, word.length + i) ===
words.slice(i + word.length, i + word.length * 2)
) {
count++;
// console.log("continue", $1, i);
return "";
} else if (
(i !== 0 && i + word.length !== words.indexOf(word, i + 1)) ||
words.slice(i, word.length + i) !==
words.slice(i + word.length, i + word.length * 2)
) {
// console.log("done", word, i);
count++;
let temp = count;
count = 0;
return (temp > 1 ? temp : "") + $1;
} else {
count = 0;
// console.log("jump to other word");
return $1;
}
});
// console.log("counted", counted);
words = counted;
}
// return words;
const split = words.slice(0, words.match(/[0-9]/)?.index || 0);
console.log("words", words);
console.log("words2", window.parent.completeFiltered[0]);
if (findDuplicateWord(split).length > 0) {
const split2 = words.slice(
words.lastIndexOf(window.parent.completeFiltered[0]) +
window.parent.completeFiltered[0].length
);
//console.log("words", words);
//console.log("third", split2);
const secondCompress = compress(split);
//const thirdCompress = split2?compress(split2):'';
//console.log(thirdCompress);
const done = words.slice(
words.match(/[0-9]/).index
//words.lastIndexOf(
// window.parent.completeFiltered[0]
//) +
// window.parent.completeFiltered[0].length
);
console.log(
"done2",
words.lastIndexOf(window.parent.completeFiltered[0]) +
window.parent.completeFiltered[0].length
);
console.log("done", done);
console.log("done", secondCompress);
if (done && secondCompress) {
words = secondCompress + done; //+ thirdCompress;
}
}
result.innerHTML = s ? words : "";
highlight(result.innerHTML);
return words;
}
function findDuplicateWord(s) {
let selector = [];
for (let q = 1; q < s.length; q++) {
for (let i = 0; i < s.length; i++) {
const sliced = s.slice(i, i + q);
const next = s.slice(i + q, i + q + q);
if (sliced.length < q) continue;
if (
s.match(new RegExp(sliced, "g")).length > 1 &&
next === sliced
) {
selector.push(sliced);
}
}
}
/* 마지막 문자가 제일 압축률이 큼. 혹은 길이가 같은 문자가 더 있다면 모두 추출 */
const filterSelector = duplicateItemRemove(selector);
const filtereds = filterSelector.filter(
(item) =>
item.length ===
selector[selector.length - 1].length /* || item.length > 1 */ ||
!!filterSelector.find((it) => {
// console.log("filter word", item);
return (
!it.includes(item) ||
(it.match(new RegExp(`(${item})`, "g")).length || 0) <= 1
);
})
);
/* 마지막 문자보다 더 짧은 문자 중 마지막 문자에 포함되는 문자가 있을 시 배제한다. */
const filtered = duplicateItemRemove(
filtereds.map((filter) => duplicateRemove(filter))
);
const completeFiltered = filtered.filter((item, i) => {
const currentIndex = filtered.indexOf(item);
const others = filtered
.slice(0, currentIndex)
.concat(...filtered.slice(currentIndex + 1));
return (
filtered[i] === filtered[filtered.length - 1] ||
others
.reverse()
.every((it) => !it.includes(duplicateRemove(item))) ||
others
.reverse()
.every(
(it) =>
!it.includes(item) ||
(it.includes(item) &&
(it.match(new RegExp(`(${item})`, "g"))?.length || 0) <=
1 &&
item.length === 1)
)
);
});
console.log("complete filtered", completeFiltered);
// if (!window.parent.completeFiltered) {
// window.parent.completeFiltered = [];
// }
window.parent.completeFiltered.push(...completeFiltered);
console.log(completeFiltered);
return completeFiltered;
}
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const temp = [];
console.log("텍스트를 입력하세요.");
rl.on("line", (line) => {
if (line.match("x")) {
rl.close();
}
temp.push(line);
});
rl.on("close", () => {
temp.forEach((line) => {
console.log("입력 라인", line);
const rs = compress(line);
console.log("압축 결과:", rs);
});
});
/* 개선 완료 */