ํƒœ๊ทธ ๋ณด๊ด€๋ฌผ: code-challenge

code-challenge

๋Œ€๋žต Moby Dick ์ž‘์„ฑ ๋ฐ›๊ธฐ ์ „์— ํ•ญ์ƒ 1 ๋ฐ”์ดํŠธ์˜

๋‹ค์Œ์€ Herman Melville์˜ Moby-Dick ์˜ ํ…์ŠคํŠธ๋ฅผ ํฌํ•จ ํ•˜๋Š” 1.2Mb ASCII ํ…์ŠคํŠธ ํŒŒ์ผ ์ž…๋‹ˆ๋‹ค . ๋˜๋Š”, ๊ณ ๋ž˜ . ๋‹น์‹ ์˜ ์ž„๋ฌด๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ฌธ์ž ์”ฉ์ด ํŒŒ์ผ์„ ์ œ๊ณต ํ•  ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ํ•จ์ˆ˜ (๋˜๋Š” ํด๋ž˜์Šค ๋“ฑ-์•„๋ž˜ ์ฐธ์กฐ)๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ ๊ฐ ๋‹จ๊ณ„์—์„œ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์ถ”์ธกํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ด๊ฒƒ์€ ์ž…๋‹ˆ๋‹ค. ๋‹น์‹ ์˜ ์ ์ˆ˜๋Š”

2*L + E

์–ด๋””๋Š” L๋ฐ”์ดํŠธ ์ œ์ถœ์˜ ํฌ๊ธฐ์ด๋ฉฐ, E๊ทธ๊ฒƒ์€ ์ž˜๋ชป ์ถ”์ธก ๋ฌธ์ž ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ๋‚ฎ์€ ์ ์ˆ˜๊ฐ€ ์ด๊น๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์„ธ๋ถ€ ์‚ฌํ•ญ

์ œ์ถœ๋ฌผ์€ ์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœ๋˜๊ฑฐ๋‚˜ ํ˜ธ์ถœ๋˜๊ฑฐ๋‚˜ ์ „์†ก๋˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๋˜๋Š” ๊ธฐ๋Šฅ ๋“ฑ์ž…๋‹ˆ๋‹ค. (1215235 ๋ฒˆ ์ •ํ™•ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.) n ๋ฒˆ์งธ ์‹œ๊ฐ„ ๋™์•ˆ ํ˜ธ์ถœ๋˜๋ฉด n ๋ฒˆ์งธ ๋ฌธ์ž whale.txt๋˜๋Š” whale2.txt( n + 1 ) ๋ฒˆ์งธ ๋ฌธ์ž์— ๋Œ€ํ•œ ์ถ”์ธก ๊ฐ’์„ ์ถœ๋ ฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค . E์ ์ˆ˜ ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋Š” ์ž˜๋ชป ์ถ”์ธก ํ•œ ์ด ๋ฌธ์ž ์ˆ˜์ž…๋‹ˆ๋‹ค.

๋Œ€๋ถ€๋ถ„์˜ ์ œ์ถœ์€ ํ˜ธ์ถœ๊ฐ„์— ๋ช‡ ๊ฐ€์ง€ ์ƒํƒœ๋ฅผ ์ €์žฅํ•ด์•ผํ•˜๋ฏ€๋กœ ํ˜ธ์ถœ ๋œ ํšŸ์ˆ˜์™€ ์ด์ „ ์ž…๋ ฅ์ด ๋ฌด์—‡์ธ์ง€ ์ถ”์  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์™ธ๋ถ€ ํŒŒ์ผ์— static์“ฐ๊ฑฐ๋‚˜ ์ „์—ญ ๋ณ€์ˆ˜ ๋ฅผ ์‚ฌ์šฉ ํ•˜๊ฑฐ๋‚˜ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ํด๋ž˜์Šค๋ฅผ ์ œ์ถœํ•˜๊ฑฐ๋‚˜ ์ƒํƒœ ๋ชจ๋‚˜๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์–ธ์–ด์— ์ ํ•ฉํ•œ ๋‹ค๋ฅธ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜์—ฌ์ด๋ฅผ ์ˆ˜ํ–‰ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ œ์ถœ์—๋Š” ์ฒซ ๋ฒˆ์งธ ํ˜ธ์ถœ ์ „์— ์ƒํƒœ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ์€ ๊ฒฐ์ • ๋ก ์ ์œผ๋กœ ์‹คํ–‰๋˜์–ด์•ผํ•˜๋ฏ€๋กœ ํ•ญ์ƒ ๋™์ผํ•œ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ํ•ญ์ƒ ๊ฐ™์€ ์ถ”์ธก์„ํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค (๋”ฐ๋ผ์„œ ํ•ญ์ƒ ๊ฐ™์€ ์ ์ˆ˜๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค).

๋‹ต๋ณ€์—๋Š” ์ œ์ถœ๋ฌผ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ E์ ์ˆ˜ ์˜ ์ผ๋ถ€ ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋„ ํฌํ•จํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค . ์ œ์ถœ ํ•œ ์–ธ์–ด์™€ ๋™์ผํ•œ ์–ธ์–ด๋กœ ์ž‘์„ฑ ๋  ํ•„์š”๋Š” ์—†์œผ๋ฉฐ ๋ฐ”์ดํŠธ ์ˆ˜์— ํฌํ•จ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฝ์„ ์ˆ˜ ์žˆ๋„๋กํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

์ œ์ถœ๊ณผ์ด ์ ์ˆ˜ ๊ณ„์‚ฐ ํ”„๋กœ๊ทธ๋žจ ๊ฐ„์˜ ์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ด€๋ จํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์ด ๋‹ค์Œ ๋ฐ”์ดํŠธ ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์ „์— ํ•ญ์ƒ 1 ๋ฐ”์ดํŠธ์˜ ์ถœ๋ ฅ์„ ์ œ๊ณตํ•˜๋Š” ํ•œ ์•„๋ฌด ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด, ๋ชจ๋“  ์ž…๋ ฅ์„ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ•˜๊ณ  ๋ชจ๋“  ์ถœ๋ ฅ์„ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋‹ค์‹œ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.)

์ถœํ’ˆ์ž‘์„ ์ œ์ถœํ•˜๊ธฐ ์ „์— ์‹œํ—˜ ํ”„๋กœ๊ทธ๋žจ์„ ์‹ค์ œ๋กœ ์‹คํ–‰ํ•˜๊ณ  ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ / ํ™•์ธํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ œ์ถœ๋ฌผ์ด ์ ์ˆ˜๋ฅผ ํ™•์ธํ•˜๊ธฐ์— ๋„ˆ๋ฌด ๋Š๋ฆฌ๊ฒŒ ์ง„ํ–‰๋˜๋Š” ๊ฒฝ์šฐ ์›์น™์ ์œผ๋กœ ์ ์ˆ˜๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ๋”๋ผ๋„ ๊ฒฝ์Ÿ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

L์ ์ˆ˜ ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋Š” ์ฝ”๋“œ ๊ณจํ”„ ์ฑŒ๋ฆฐ์ง€์— ๋Œ€ํ•œ ์ผ๋ฐ˜์ ์ธ ๊ทœ์น™์— ๋”ฐ๋ผ ๊ณ„์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์ œ์ถœ๋ฌผ์— ์—ฌ๋Ÿฌ ํŒŒ์ผ์ด ํฌํ•จ ๋œ ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฒฝ์šฐ ์ ์ˆ˜ ๋ฐ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ ๊ทœ์น™์„ ์ฐธ๊ณ ํ•˜์‹ญ์‹œ์˜ค . ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ L์ ์ˆ˜์— ํฌํ•จ๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค .

๊ธฐ์กด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์ง€๋งŒ ๋‹ค๋ฅธ ์™ธ๋ถ€ ํŒŒ์ผ์„๋กœ๋“œ ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ์ฝ”๋“œ๊ฐ€ whale.txt๋˜๋Š”whale2.txt์œ„์—์„œ ์„ค๋ช…ํ•œ ๋ฐฉ๋ฒ• ์ด์™ธ์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ํŒŒ์ผ์„ ์ €์žฅํ•˜์‹ญ์‹œ์˜ค. ์‚ฌ์ „ ํ›ˆ๋ จ ๋œ ์‹ ๊ฒฝ๋ง ๋˜๋Š” ๊ธฐํƒ€ ํ†ต๊ณ„ ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ๋กœ๋“œ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์‹ ๊ฒฝ๋ง์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์ง€๋งŒ ์ œ์ถœ์— ๊ฐ€์ค‘์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จ์‹œํ‚ค๊ณ  ๋ฐ”์ดํŠธ ์ˆ˜๋กœ ๊ณ„์‚ฐํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ์ด์œ ๋กœ ์–ธ์–ด ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— Moby Dick์˜ ์ผ๋ถ€ ๋˜๋Š” ๋ชจ๋“  ํ…์ŠคํŠธ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ด ํฌํ•จ ๋œ ๊ฒฝ์šฐ ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋„ ์–ธ์–ด ๋˜๋Š” ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ผ๋ถ€์ธ ํ•œ ํ…์ŠคํŠธ ์ฒ˜๋ฆฌ, ์˜ˆ์ธก ๋˜๋Š” ์••์ถ•๊ณผ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•˜์—ฌ ์›ํ•˜๋Š” ๋‹ค๋ฅธ ๋‚ด์žฅ ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ†ต๊ณ„ ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ํฌํ•จํ•˜๋Š”๋ณด๋‹ค ์ด๊ตญ์ ์ธ ํŠน์ˆ˜ ๋ฃจํ‹ด์˜ ๊ฒฝ์šฐ์ด๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๊ณ  ๋ฐ”์ดํŠธ ์ˆ˜์— ํฌํ•จ์‹œ์ผœ์•ผํ•ฉ๋‹ˆ๋‹ค.

์ผ๋ถ€ ์ œ์ถœ์—๋Š” ์ฝ”๋“œ์— ์˜ํ•ด ์ž์ฒด์ ์œผ๋กœ ์ƒ์„ฑ ๋œ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ ๋œ ์ฝ”๋“œ๋ฅผ ๋‹ต๋ณ€์— ํฌํ•จ์‹œํ‚ค๊ณ  ์ž‘๋™ ๋ฐฉ์‹์„ ์„ค๋ช…ํ•˜์‹ญ์‹œ์˜ค . ์ œ์ถœ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐ์ด ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ”์ดํŠธ ์ˆ˜์— ํฌํ•จ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ก์ ์ธ ์ด์œ ๋กœ ํŒŒ์ผ์—๋Š” ๋‘ ๊ฐ€์ง€ ๋ฒ„์ „์ด ์žˆ์œผ๋ฉฐ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ฅผ ๋‹ต๋ณ€์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—์„œ whale2.txt์ค„ ๋ฐ”๊ฟˆ ๋ฌธ๋‹จ์˜ ๋์— ๋งŒ ๋‚˜ํƒ€๋‚˜๋„๋ก (์œ„ ๋งํฌ) ํ…์ŠคํŠธ๋Š”, ํฌ์žฅ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์›๋ณธ whale.txt์—์„œ ํ…์ŠคํŠธ๋Š” 74 ์ž ๋„ˆ๋น„๋กœ ์ค„ ๋ฐ”๊ฟˆ๋˜๋ฏ€๋กœ ๊ฐ ์ค„์˜ ๋์„ ์˜ˆ์ธกํ•˜๊ณ  ํ…์ŠคํŠธ๋ฅผ ์˜ˆ์ธกํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ๋„์ „์ด ๋” ์–ด๋ ค์›Œ ์ง€๋ฏ€๋กœ whale2.txt์ƒˆ๋กœ์šด ๋‹ต๋ณ€์— ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. ๋‘ ํŒŒ์ผ์˜ ํฌ๊ธฐ๋Š” 1215236 ๋ฐ”์ดํŠธ์ž…๋‹ˆ๋‹ค.


์š”์•ฝํ•˜๋ฉด ๋ชจ๋“  ๋‹ต๋ณ€์—๋Š” ๋‹ค์Œ ์‚ฌํ•ญ์ด ํฌํ•จ๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

  • ์ œ์ถœ ์ž์ฒด. (์ฝ”๋“œ์™€ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํŒŒ์ผ-ํฌ๋ฉด ๋งํฌ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)
  • ์ฝ”๋“œ ์ž‘๋™ ๋ฐฉ์‹์— ๋Œ€ํ•œ ์„ค๋ช… ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ I / O ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜์‹ญ์‹œ์˜ค. ๊ท€ํ•˜์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์ค‘์š”ํ•˜๋ฉฐ, ์ข‹์€ ์„ค๋ช…์€ ์ €์—๊ฒŒ ๋ฐ”์šดํ‹ฐ๋ฅผ ์–ป๊ฒŒ๋ฉ๋‹ˆ๋‹ค.
  • ์ ์ˆ˜๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. (์ด๊ฒƒ์ด ์ด์ „ ๋‹ต๋ณ€๊ณผ ๋™์ผํ•˜๋ฉด ๋งํฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)
  • ์ œ์ถœ๋ฌผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ ๋ฐ ํ•ด๋‹น ์ฝ”๋“œ์— ๋Œ€ํ•œ ์„ค๋ช… ์—ฌ๊ธฐ์—๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ตœ์ ํ™”ํ•˜๊ณ  ๋ฐ์ดํ„ฐ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค (๋ฐ”์ดํŠธ ์ˆ˜์—๋Š” ํฌํ•จ๋˜์ง€ ์•Š์ง€๋งŒ ๋‹ต๋ณ€์— ํฌํ•จ๋˜์–ด์•ผ ํ•จ).

๋ฆฌ๋” ๋ณด๋“œ

var QUESTION_ID=152856,OVERRIDE_USER=21034;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:380px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Score</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

๋ฐ”์šดํ‹ฐ

๋•Œ๋•Œ๋กœ ๋‚˜๋Š” ๋‹ค๋ฅธ ์ ‘๊ทผ๋ฒ•์„ ์žฅ๋ คํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ”์šดํ‹ฐ๋ฅผ ์ œ๊ณต ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ 50 ์ ์€ A. Rex์— ์ˆ˜์—ฌ๋˜์—ˆ์œผ๋ฉฐ ๋‹น์‹œ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ 100 ์ ๋„ A. Rex์—๊ฒŒ๋„ ์ฃผ์–ด์กŒ์œผ๋ฉฐ, ๊ทธ๋“ค์€ ๊ธฐ์กด ๋‹ต๋ณ€์— ์•„์ฃผ ์ข‹์€ ์„ค๋ช…์„ ์ถ”๊ฐ€ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ๋‹ต๋ณ€์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ํ˜„์ƒ๊ธˆ ์ธ 200 ์  ์€

  • ์ƒˆ๋กœ์šด ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์Ÿ์ ์ธ ๋‹ต๋ณ€. (์ด๊ฒƒ์€ ํ˜„์ƒ๊ธˆ์— ๋“ค์–ด๊ฐ€๋Š” ๋‚˜์˜ ๋‹ด๋‹น์ž์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜์˜ ์ฃผ๊ด€์ ์ธ ํŒ๋‹จ์— ๊ทผ๊ฑฐ ํ•  ๊ฒƒ์ด์ง€๋งŒ, ๋‹น์‹ ์€ ์ €๋ฅผ ๊ณตํ‰ํ•˜๊ฒŒ ๋ฏฟ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹ ์˜ ๋Œ€๋‹ต์€ ๊ทธ๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์ถฉ๋ถ„ํ•œ ์„ค๋ช…์„ ํฌํ•จํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค!) ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ํš๋“ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด ๋‹ต๋ณ€๊ณผ ๋น„๊ตํ•˜์—ฌ ํ•ฉ๋ฆฌ์ ์œผ๋กœ ์ž˜ ์ˆ˜ํ–‰ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๋ฐ˜๋ณต์  ์ธ ์‹ ๊ฒฝ๋ง์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์†”๋ฃจ์…˜์„๋ณด๊ณ  ์‹ถ์–ดํ•˜์ง€๋งŒ ํ˜„์žฌ ์ตœ๊ณ  ์ ์ˆ˜๋ฅผ ์ฐจ์ง€ํ•˜๋Š” Markov ๋ชจ๋ธ๊ณผ๋Š” ๋‹ค๋ฅธ ๊ฒƒ์œผ๋กœ ๋ฐ”์šดํ‹ฐ๋ฅผ ์ˆ˜์—ฌํ•ฉ๋‹ˆ๋‹ค.

๋˜๋Š”:

  • ์–ด๋–ค ๋ฐฉ๋ฒ• ์œผ๋กœ๋“  A. Rex์˜ ์ตœ๊ณ  ์ ์ˆ˜ (ํ˜„์žฌ 444444)๋ฅผ์ด๊ธฐ๋Š” ์‚ฌ๋žŒ.

200 ํฌ์ธํŠธ ํ˜„์ƒ๊ธˆ์ด ์ฒญ๊ตฌ๋˜๋ฉด 400 ํฌ์ธํŠธ 1์„ ์ œ๊ณตํ•˜๊ณ  ๊ทธ์— ๋”ฐ๋ผ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.



๋‹ต๋ณ€

/// , 2 * 1 + 1020874 = 1020876

 

๊ณต๋ฐฑ์„ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.


๋‹ต๋ณ€

Node.js, 2 * 224 + 524279 = 524727

์ ์ˆ˜ ์—…๋ฐ์ดํŠธ์— ๋Œ€ํ•ด์„œ๋Š”์ด ๊ฒŒ์‹œ๋ฌผ ๋์—์žˆ๋Š” ๋ณ€๊ฒฝ ๋กœ๊ทธ๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

๋ฐ”์ดํŠธ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

a=[...l='14210100'],m={},s={},b={}
f=c=>a.some((t,n)=>x=s[y=l.slice(n)]>t|/^[A-Z '"(]/.test(y)&&b[y],l+=String.fromCharCode(c),a.map((_,n)=>(m[x=l.slice(n)]=-~m[x])<s[y=l.slice(n,8)]||(s[y]=m[x],b[y]=c)),l=l.slice(1))&&x||32

๋งˆ์ง€๋ง‰ 8์ž๋ฅผ๋ณด๊ณ  ๋‹ค์Œ์„ ์˜ˆ์ธก ํ•˜๋Š” ๊ฐ„๋‹จํ•œ PPM ๋ชจ๋ธ ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค .

T ๋Š” ์ ์–ด๋„ T [L] ๋ฒˆ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๊ธธ์ด L ์˜ ํŒจํ„ด์„ ์‹ ๋ขฐํ•ฉ๋‹ˆ๋‹ค . ์—ฌ๊ธฐ์„œ T ๋Š” ์ž„์˜์˜ ์ž„๊ณ„ ๊ฐ’์˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค : [1,1,2,1,2,3,5,2] . ๋˜ํ•œ ์šฐ๋ฆฌ๋Š” ํ•ญ์ƒ ์ฒซ ๋ฌธ์ž๊ฐ€ ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์„ ์‹ ๋ขฐํ•ฉ๋‹ˆ๋‹ค .[A-Z '"(]

๊ฐ€์žฅ ๊ธด ์‹ ๋ขฐํ•  ์ˆ˜์žˆ๋Š” ํŒจํ„ด์„ ์„ ํƒํ•˜๊ณ  ํ†ตํ™”์‹œ์ด ํŒจํ„ด๊ณผ ๊ด€๋ จ๋œ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋กœ ์˜ˆ์ธก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋…ธํŠธ

  • ์†๋„์— ์ตœ์ ํ™”๋˜์ง€๋Š” ์•Š์•˜์ง€๋งŒ ๋žฉํ†ฑ์—์„œ ์•ฝ 15 ์ดˆ ์•ˆ์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

  • ๋ชจ๋ธ์„ ์žฌ์„ค์ •ํ•˜์ง€ ์•Š๊ณ  ํ”„๋กœ์„ธ์Šค๋ฅผ ์—ฐ์†์œผ๋กœ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณต ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด 5 ๋ฒˆ ๋ฐ˜๋ณต ํ•œ ํ›„ ์˜ค๋ฅ˜ ์ˆ˜๊ฐ€ ~ 268000์œผ๋กœ ์ˆ˜๋ ด๋ฉ๋‹ˆ๋‹ค.

  • ์˜ˆ์ธก ํ•จ์ˆ˜์˜ ํ˜„์žฌ ์„ฑ๊ณต๋ฅ ์€ ~ 56.8 %์ž…๋‹ˆ๋‹ค. ์˜๊ฒฌ์—์„œ @immibis๊ฐ€ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด, ์ž˜๋ชป๋œ ์ถ”์ธก๊ณผ ์˜ฌ๋ฐ”๋ฅธ ์ถ”์ธก์ด ํ˜ผํ•ฉ๋˜์–ด ์žˆ์œผ๋ฉด ๊ฒฐ๊ณผ๋ฅผ ๊ฑฐ์˜ ์ฝ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด ์ฑ… ๋ ๋ถ€๋ถ„์—์žˆ๋Š”์ด ์Šค ๋‹ˆํŽซ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    Here be it said, that this pertinacious pursuit of one particular whale,[LF]
    continued through day into night, and through night into day, is a thing[LF]
    by no means unprecedented in the South sea fishery.
    

    ๋œ๋‹ค :

    "e e be it said, that thes woacangtyous sarsuet of tie oort cular thale[LF][LF]
     orsinued toeough tir on e togh   and sheough toght an o ters af t shin[LF][LF]
    be to means insrocedented tn hhe sputh Sevsaonh ry,
    

    ์ž˜๋ชป๋œ ์ถ”์ธก์„ ๋ฐ‘์ค„๋กœ ๋ฐ”๊พธ๋ฉด ํ•จ์ˆ˜์˜ ์˜ฌ๋ฐ”๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋” ์ž˜ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    _e_e be it said, that th_s _____n___ous __rsu_t of __e __rt_cular _hale_[LF]
    _o__inued t__ough ___ _n__ __gh__ and _h_ough __ght _n_o ____ __ _ _hin_[LF]
    b_ _o means _n_r_cedented _n _he __uth _e_____h_ry_
    

    NB : ์œ„ ์˜ˆ์ œ๋Š” ์ด์ „ ๋ฒ„์ „์˜ ์ฝ”๋“œ๋กœ ์ž‘์„ฑ๋˜์—ˆ์œผ๋ฉฐ, ์ž…๋ ฅ ํŒŒ์ผ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฒ„์ „์—์„œ ์ž‘์—…ํ•ฉ๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ

/**
  The prediction function f() and its variables.
*/
a=[...l='14210100'],m={},s={},b={}
f=c=>a.some((t,n)=>x=s[y=l.slice(n)]>t|/^[A-Z '"(]/.test(y)&&b[y],l+=String.fromCharCode(c),a.map((_,n)=>(m[x=l.slice(n)]=-~m[x])<s[y=l.slice(n,8)]||(s[y]=m[x],b[y]=c)),l=l.slice(1))&&x||32

/**
  A closure containing the test code and computing E.
  It takes f as input.
  (f can't see any of the variables defined in this scope.)
*/
;
(f => {
  const fs = require('fs');

  let data = fs.readFileSync('whale2.txt'),
      len = data.length,
      err = 0;

  console.time('ElapsedTime');

  data.forEach((c, i) => {
    i % 100000 || console.log((i * 100 / len).toFixed(1) + '%');

    if(i < len - 1 && f(c) != data[i + 1]) {
      err++;
    }
  })

  console.log('E = ' + err);
  console.timeEnd('ElapsedTime');
})(f)

๋ณ€๊ฒฝ ๋กœ๊ทธ

  • 524727 -whale2.txt๋กœ ์ „ํ™˜ํ•˜์—ฌ 19644 ํฌ์ธํŠธ ์ ˆ์•ฝ (์ฑŒ๋ฆฐ์ง€ ์—…๋ฐ์ดํŠธ)
  • 544371- ๋Œ€๋ฌธ์ž, ๋”ฐ์˜ดํ‘œ, ํฐ ๋”ฐ์˜ดํ‘œ ๋˜๋Š” ์—ฌ๋Š” ๊ด„ํ˜ธ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํŒจํ„ด์„ ํ•ญ์ƒ ์‹ ๋ขฐํ•˜๋„๋กํ•˜์—ฌ 327 ์ ์„ ์ ˆ์•ฝํ–ˆ์Šต๋‹ˆ๋‹ค.
  • 544698- ๊ณต๋ฐฑ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํŒจํ„ด์„ ํ•ญ์ƒ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋„๋กํ•˜์—ฌ 2119 ํฌ์ธํŠธ ์ ˆ์•ฝ
  • 546817- ์ž„๊ณ„ ๊ฐ’์„ ์กฐ์ •ํ•˜๊ณ  ์˜ˆ์ธก ๊ธฐ๋Šฅ์„ ๊ณจํ”„ํ™” ํ•˜์—ฌ 47 ์ ์„ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค.
  • 546864- ์ตœ๋Œ€ ํŒจํ„ด ๊ธธ์ด๋ฅผ 8 ์ž๋กœ ํ™•์žฅํ•˜์—ฌ 1496 ํฌ์ธํŠธ ์ ˆ์•ฝ
  • 548360- ์‹ ๋ขฐํ•  ์ˆ˜์žˆ๋Š” ํŒจํ„ด์˜ ๊ฐœ๋…์„ ๋„์ž…ํ•˜์—ฌ ๊ธธ์ด์— ๋”ฐ๋ฅธ ์ž„๊ณ„ ๊ฐ’์œผ๋กœ 6239 ํฌ์ธํŠธ๋ฅผ ์ ˆ์•ฝํ–ˆ์Šต๋‹ˆ๋‹ค.
  • 554599- ์ค„ ๋ฐ”๊ฟˆ ์˜ˆ์ธก์„ ๊ฐœ์„ ํ•˜์—ฌ 1030 ์ ์„ ์ ˆ์•ฝํ–ˆ์Šต๋‹ˆ๋‹ค.
  • 555629- ์˜ˆ์ธก ๊ธฐ๋Šฅ์„ ๊ณจํ”„๋กœํ•˜์—ฌ 22 ํฌ์ธํŠธ ์ ˆ์•ฝ
  • 555651- ์˜ˆ์ธก ๊ธฐ๋Šฅ์„ ๊ณจํ”„๋กœํ•˜์—ฌ 40 ์ ์„ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค
  • 555691- ์ดˆ๊ธฐ ์ ์ˆ˜

๋‹ต๋ณ€

ํŽ„, 2 ยท 70525 + 326508 = 467558

์˜ˆ์–ธ์ž

$m=($u=1<<32)-1;open B,B;@e=unpack"C*",join"",<B>;$e=2903392593;sub u{int($_[0]+($_[1]-$_[0])*pop)}sub o{$m&(pop()<<8)+pop}sub g{($h,%m,@b,$s,$E)=@_;if($d eq$h){($l,$u)=(u($l,$u,$L),u($l,$u,$U));$u=o(256,$u-1),$l=o($l),$e=o(shift@e,$e)until($l^($u-1))>>24}$M{"@c"}{$h}++-++$C{"@c"}-pop@c for@p=($h,@c=@p);@p=@p[0..19]if@p>20;@c=@p;for(@p,$L=0){$c="@c";last if" "ne pop@c and@c<2 and$E>99;$m{$_}+=$M{$c}{$_}/$C{$c}for sort keys%{$M{$c}};$E+=$C{$c}}$s>5.393*$m{$_}or($s+=$m{$_},push@b,$_)for sort{$m{$b}<=>$m{$a}}sort keys%m;$e>=u($l,$u,$U=$L+$m{$_}/$s)?$L=$U:return$d=$_ for sort@b}

์ด ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ด ํŒŒ์ผ์ด ํ•„์š” ํ•ฉ๋‹ˆ๋‹ค (์—ฌ๊ธฐ์„œ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์•ผ ํ•จ) B. ์œ„ ๋ฌธ์ž ์˜ ๋‘ ๋ฒˆ์งธ ์ธ์Šคํ„ด์Šค ์—์„œ์ด ํŒŒ์ผ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค B.์ด ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

์ด ํ”„๋กœ๊ทธ๋žจ์€ ๋ณธ์งˆ์ ์œผ๋กœ user2699์˜ ๋‹ต๋ณ€ ์—์„œ์™€ ๊ฐ™์ด Markov ๋ชจ๋ธ์˜ ์กฐํ•ฉ์„ ์‚ฌ์šฉ ํ•˜์ง€๋งŒ ์•ฝ๊ฐ„์˜ ์ˆ˜์ •์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ๋‹ค์Œ ์บ๋ฆญํ„ฐ์— ๋Œ€ํ•œ ๋ถ„ํฌ ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค . ์šฐ๋ฆฌ๋Š” ์ •๋ณด ์ด๋ก ์„ ์‚ฌ์šฉํ•˜์—ฌ B์ธ์ฝ”๋”ฉ ํžŒํŠธ ์— ์˜ค๋ฅ˜๋ฅผ ํ—ˆ์šฉํ• ์ง€ ๋˜๋Š” ์Šคํ† ๋ฆฌ์ง€ ๋น„ํŠธ๋ฅผ ์†Œ๋น„ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค (์žˆ๋Š” ๊ฒฝ์šฐ). ์šฐ๋ฆฌ๋Š” ์‚ฐ์ˆ  ์ฝ”๋”ฉ ์„ ์‚ฌ์šฉ ํ•˜์—ฌ ๋ชจ๋ธ์˜ ์†Œ์ˆ˜ ๋น„ํŠธ๋ฅผ ์ตœ์ ์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ์˜ ๊ธธ์ด๋Š” 582 ๋ฐ”์ดํŠธ (๋ถˆํ•„์š”ํ•œ ์ตœ์ข… ๊ฐœํ–‰ ํฌํ•จ)์ด๊ณ  ์ด์ง„ ํŒŒ์ผ์˜ B๊ธธ์ด๋Š” 69942 ๋ฐ”์ดํŠธ์ด๋ฏ€๋กœ ์—ฌ๋Ÿฌ ํŒŒ์ผ์˜ ์ ์ˆ˜ ๋ฅผ ๋งค๊ธฐ๋Š” ๊ทœ์น™์—L ๋”ฐ๋ผ 582 + 69942 + 1 = 70525 ๋กœ ์ ์ˆ˜ ๊ฐ€ ๋งค๊ฒจ์ง‘๋‹ˆ๋‹ค.

์ด ํ”„๋กœ๊ทธ๋žจ์—๋Š” ๊ฑฐ์˜ ํ™•์‹คํ•˜๊ฒŒ 64 ๋น„ํŠธ (little-endian?) ์•„ํ‚คํ…์ฒ˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. m5.largeAmazon EC2 ์˜ ์ธ์Šคํ„ด์Šค์—์„œ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ์•ฝ 2.5 ๋ถ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค .

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ

# Golfed submission
require "submission.pl";

use strict; use warnings; use autodie;

# Scoring length of multiple files adds 1 penalty
my $length = (-s "submission.pl") + (-s "B") + 1;

# Read input
open my $IN, "<", "whale2.txt";
my $input = do { local $/; <$IN> };

# Run test harness
my $errors = 0;
for my $i ( 0 .. length($input)-2 ) {
    my $current = substr $input, $i, 1;
    my $decoded = g( $current );

    my $correct = substr $input, $i+1, 1;
    my $error_here = 0 + ($correct ne $decoded);
    $errors += $error_here;
}

# Output score
my $score = 2 * $length + $errors;
print <<EOF;
length $length
errors $errors
score  $score
EOF

ํ…Œ์ŠคํŠธ ํ•˜๋„ค์Šค๋Š” ์ œ์ถœ์ด ํŒŒ์ผ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ • submission.plํ•˜์ง€๋งŒ ๋‘ ๋ฒˆ์งธ ์ค„์—์„œ ์‰ฝ๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ…์ŠคํŠธ ๋น„๊ต

"And did none of ye see it before?" cried Ahab, hailing the perched men all around him.\\"I saw him almost that same instant, sir, that Captain
"And wid note of te fee bt seaore   cried Ahab, aasling the turshed aen inl atound him. \"' daw him wsoost thot some instant, wer, that Saptain
"And _id no_e of _e _ee _t _e_ore__ cried Ahab, _a_ling the __r_hed _en __l a_ound him._\"_ _aw him ___ost th_t s_me instant, __r, that _aptain

Ahab did, and I cried out," said Tashtego.\\"Not the same instant; not the same--no, the doubloon is mine, Fate reserved the doubloon for me. I
Ahab aid  ind I woued tut,  said tashtego, \"No, the same instant, tot the same -tow nhe woubloon ws mane. alte ieserved the seubloon ior te, I
Ahab _id_ _nd I ___ed _ut,_ said _ashtego__\"No_ the same instant_ _ot the same_-_o_ _he _oubloon _s m_ne_ __te _eserved the __ubloon _or _e_ I

only; none of ye could have raised the White Whale first. There she blows!--there she blows!--there she blows! There again!--there again!" he cr
gnly  towe of ye sould have tersed the shite Whale aisst  Ihere ihe blows! -there she blows! -there she blows! Ahere arains -mhere again!  ce cr
_nly_ _o_e of ye _ould have ___sed the _hite Whale _i_st_ _here _he blows!_-there she blows!_-there she blows! _here a_ain__-_here again!_ _e cr

์ด ์ƒ˜ํ”Œ ( ๋‹ค๋ฅธ ๋‹ต๋ณ€ ์—์„œ ์„ ํƒ๋จ )์€ ํ…์ŠคํŠธ์—์„œ ๋‹ค์†Œ ๋Šฆ๊ฒŒ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ์ด ์‹œ์ ์—์„œ ๋ชจ๋ธ์ด ์ƒ๋‹นํžˆ ๊ฐœ๋ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋ธ์€ ์บ๋ฆญํ„ฐ๋ฅผ ์ถ”์ธกํ•˜๋Š” ๋ฐ ๋„์›€์ด๋˜๋Š” 70 ํ‚ฌ๋กœ๋ฐ”์ดํŠธ์˜ โ€œํžŒํŠธโ€์— ์˜ํ•ด ํ™•์žฅ๋ฉ๋‹ˆ๋‹ค. ๋‹จ์ˆœํžˆ ์œ„์˜ ์งง์€ ์ฝ”๋“œ ์Šค ๋‹ˆํŽซ์œผ๋กœ ๊ตฌ๋™๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.

ํžŒํŠธ ์ƒ์„ฑ

๋‹ค์Œ ํ”„๋กœ๊ทธ๋žจ์€ ์œ„์˜ ์ •ํ™•ํ•œ ์ œ์ถœ ์ฝ”๋“œ (ํ‘œ์ค€ ์ž…๋ ฅ)๋ฅผ ์ˆ˜๋ฝํ•˜๊ณ  ์œ„์˜ ์ •ํ™•ํ•œ BํŒŒ์ผ (ํ‘œ์ค€ ์ถœ๋ ฅ)์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค .

@S=split"",join"",<>;eval join"",@S[0..15,64..122],'open W,"whale2.txt";($n,@W)=split"",join"",<W>;for$X(0..@W){($h,$n,%m,@b,$s,$E)=($n,$W[$X]);',@S[256..338],'U=0)',@S[343..522],'for(sort@b){$U=($L=$U)+$m{$_}/$s;if($_ eq$n)',@S[160..195],'X<128||print(pack C,$l>>24),',@S[195..217,235..255],'}}'

์œ ์‚ฌํ•œ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ œ์ถœ๋งŒํผ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ๊ฑฐ์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.

์„ค๋ช…

์ด ์„น์…˜์—์„œ๋Š”์ด ์†”๋ฃจ์…˜์ด ์ˆ˜ํ–‰ํ•˜๋Š” ์ž‘์—…์„ ์‚ฌ์šฉ์ž๊ฐ€ โ€œ์ง‘์—์„œ ์‹œ๋„โ€ํ•  ์ˆ˜์žˆ์„๋งŒํผ ์ถฉ๋ถ„ํžˆ ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๋ ค๊ณ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹ต๋ณ€์„ ๋‹ค๋ฅธ ๋‹ต๋ณ€๊ณผ ๊ตฌ๋ณ„ํ•˜๋Š” ์ฃผ์š” ๊ธฐ์ˆ ์€ โ€œ๋˜๊ฐ๊ธฐโ€๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ ๋ถ„๋ฅ˜๋˜๋Š” ๋ช‡ ๊ฐ€์ง€ ์„น์…˜์ด์ง€๋งŒ, ๊ฑฐ๊ธฐ์— ๋„๋‹ฌํ•˜๊ธฐ ์ „์— ๊ธฐ๋ณธ ์‚ฌํ•ญ์„ ์„ค์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋ธ

์†”๋ฃจ์…˜์˜ ๊ธฐ๋ณธ ์„ฑ๋ถ„์€ ์–ธ์–ด ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๋ชฉ์ ์„ ์œ„ํ•ด ๋ชจ๋ธ ์€ ์–ด๋Š ์ •๋„์˜ ์˜์–ด ํ…์ŠคํŠธ ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋‹ค์Œ ๋ฌธ์ž์— ๋Œ€ํ•œ ํ™•๋ฅ  ๋ถ„ํฌ ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๋•Œ ์˜์–ด ํ…์ŠคํŠธ๋Š” Moby Dick์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋Š” ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ๋ฌธ์ž์— ๋Œ€ํ•œ ๋‹จ์ผ ์ถ”์ธก์ด ์•„๋‹ˆ๋ผ ๋ถ„ํฌ ์ž…๋‹ˆ๋‹ค.

์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ, ์šฐ๋ฆฌ๋Š” ๋ณธ์งˆ์  ์œผ๋กœ user2699์— ์˜ํ•œ์ด ๋‹ต๋ณ€ ์—์„œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค . Anders Kaseorg ์˜ ์ตœ๊ณ  ์ ์ˆ˜ (์šฐ๋ฆฌ ์ž์‹ ์ด ์•„๋‹Œ)์˜ ๋ชจํ˜•์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค . ๋‹จ ํ•˜๋‚˜์˜ ์ตœ์„ ์˜ ์ถ”์ธก๋ณด๋‹ค๋Š” ๋ถ„ํฌ๋ฅผ ์ถ”์ถœ ํ•  ์ˆ˜ ์—†์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋ก ์ ์œผ๋กœ, ๊ทธ ๋Œ€๋‹ต์€ ๊ฐ€์ค‘ ๊ธฐํ•˜ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜์ง€๋งŒ, ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ํ•ด์„ํ–ˆ์„ ๋•Œ ๋‹ค์†Œ ๋‚˜์œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์—ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ โ€œ๋น„๋ฐ€ ์†Œ์Šคโ€๋Š” ๋ชจ๋ธ์ด ์•„๋‹ˆ๋ผ ์ „์ฒด์ ์ธ ์ ‘๊ทผ ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋‹ต๋ณ€์—์„œ ๋ชจ๋ธ์„ โ€œํ›”์นœโ€๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ โ€œ๋” ๋‚˜์€โ€๋ชจ๋ธ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด ๋‚˜๋จธ์ง€ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋” ๋‚˜์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋กœ, Lempel-Ziv์™€ ๊ฐ™์€ ๋Œ€๋ถ€๋ถ„์˜ ์••์ถ• ๋ฐฉ๋ฒ•์€ ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ โ€œ์–ธ์–ด ๋ชจ๋ธโ€๋กœ ๊ฐ„์ฃผ ๋  ์ˆ˜ ์žˆ์ง€๋งŒ ์•ฝ๊ฐ„ ์›€์ผœ ์ฅ์–ด ์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. (Brows-Wheeler ๋ณ€ํ™˜์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์€ ํŠนํžˆ ๊นŒ๋‹ค ๋กญ์Šต๋‹ˆ๋‹ค!) ๋˜ํ•œ user2699์˜ ๋ชจ๋ธ์€ Markov ๋ชจ๋ธ์˜ ์ˆ˜์ • ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ๋ณธ์งˆ์ ์œผ๋กœ์ด ๋„์ „์— ๋Œ€ํ•œ ๊ฒฝ์Ÿ ๋˜๋Š” ๋Œ€์ฒด๋กœ ํ…์ŠคํŠธ๋ฅผ ๋ชจ๋ธ๋งํ•˜๋Š” ๋‹ค๋ฅธ ์–ด๋–ค ๊ฒƒ๋„ ์—†์Šต๋‹ˆ๋‹ค.

์ „๋ฐ˜์ ์ธ ์•„ํ‚คํ…์ฒ˜

์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ์ „์ฒด ์•„ํ‚คํ…์ฒ˜๋ฅผ ์—ฌ๋Ÿฌ ์กฐ๊ฐ์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ตœ์ƒ์œ„ ์ˆ˜์ค€ ๊ด€์ ์—์„œ ์•ฝ๊ฐ„์˜ ์ƒํƒœ ๊ด€๋ฆฌ ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํŠน๋ณ„ํžˆ ํฅ๋ฏธ๋กญ์ง€๋Š” ์•Š์ง€๋งŒ, ์™„์ „์„ฑ์„ ์œ„ํ•ด ํ”„๋กœ๊ทธ๋žจ์ด ๋‹ค์Œ ์ถ”์ธก์„ ์š”๊ตฌํ•  ๋•Œ๋งˆ๋‹ค Moby Dick์˜ ์˜ฌ๋ฐ”๋ฅธ ์ ‘๋‘์‚ฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๊ฐ•์กฐํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ณผ๊ฑฐ์˜ ์ž˜๋ชป๋œ ์ถ”์ธก์„ ์–ด๋–ค ์‹ ์œผ๋กœ๋“  ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํšจ์œจ์„ฑ์„ ์œ„ํ•ด ์–ธ์–ด ๋ชจ๋ธ์€ ์ฒซ ๋ฒˆ์งธ N ๋ฌธ์ž์˜ ์ƒํƒœ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ (N + 1) ๋ฌธ์ž์˜ ์ƒํƒœ๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์›์น™์ ์œผ๋กœ ํ˜ธ์ถœ ๋  ๋•Œ๋งˆ๋‹ค ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ์˜ ๊ธฐ๋ณธ โ€œ๋“œ๋ผ์ด๋ฒ„โ€๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•˜๊ณ  ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์ถ”์ธกํ•˜๋Š” ๋ถ€๋ถ„์„ ๋“ค์—ฌ๋‹ค ๋ณด๋„๋กํ•ฉ์‹œ๋‹ค. ์–ธ์–ด ๋ชจ๋ธ (์œ„์—์„œ ๋…ผ์˜), โ€œํžŒํŠธโ€ํŒŒ์ผ ๋ฐ โ€œํ†ต์—ญ์‚ฌโ€์˜ ์„ธ ๋ถ€๋ถ„์„ ๊ฐœ๋…์ ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐ ๋„์›€์ด๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‹จ๊ณ„์—์„œ ํ†ต์—ญ์‚ฌ๋Š” ์–ธ์–ด ๋ชจ๋ธ์— ๋‹ค์Œ ๋ฌธ์ž์˜ ๋ฐฐํฌ๋ฅผ ์š”์ฒญํ•˜๊ณ  ํžŒํŠธ ํŒŒ์ผ์—์„œ ์ผ๋ถ€ ์ •๋ณด๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„์„ ์ถ”์ธก์œผ๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ •ํ™•ํžˆ ํžŒํŠธ ํŒŒ์ผ์— ์–ด๋–ค ์ •๋ณด๊ฐ€ ์žˆ๊ณ  ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”์ง€๋Š” ๋‚˜์ค‘์— ์„ค๋ช… ํ•  ๊ฒƒ์ด์ง€๋งŒ ์ง€๊ธˆ์€์ด ๋ถ€๋ถ„๋“ค์„ ์ •์‹ ์ ์œผ๋กœ ๋ถ„๋ฆฌ์‹œํ‚ค๋Š” ๋ฐ ๋„์›€์ด๋ฉ๋‹ˆ๋‹ค. ๊ตฌํ˜„ ์ธก๋ฉด์—์„œ ํžŒํŠธ ํŒŒ์ผ์€ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ๋ณ„๋„์˜ (์ด์ง„) ํŒŒ์ผ์ด์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ ๋‚ด๋ถ€์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์ด๊ฑฐ๋‚˜ ๋ฌด์–ธ๊ฐ€ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทผ์‚ฌ์น˜๋กœ

์ด ๋‹ต๋ณ€ ์—์„œ์ฒ˜๋Ÿผ bzip2 ์™€ ๊ฐ™์€ ํ‘œ์ค€ ์••์ถ• ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ โ€œhintsโ€ํŒŒ์ผ์€ ์••์ถ• ํŒŒ์ผ์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค. โ€œํ†ต์—ญ์‚ฌโ€๋Š” ์••์ถ• ํ•ด์ œ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ˜๋ฉด โ€œ์–ธ์–ด ๋ชจ๋ธโ€์€ ์•ฝ๊ฐ„ ์•”์‹œ ์ ์ž…๋‹ˆ๋‹ค (์œ„์—์„œ ์–ธ๊ธ‰ ํ•œ ๋ฐ”์™€ ๊ฐ™์ด).

ํžŒํŠธ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์ถ”๊ฐ€ ๋ถ„์„์„์œ„ํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ๋ฅผ ์„ ํƒํ•ด ๋ด…์‹œ๋‹ค. ํ…์ŠคํŠธ๊ฐ€ N๊ธธ๊ณ  ๊ทผ์‚ฌํ•œ ๋ฌธ์ž ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฌธ์ž๋Š” Eํ™•๋ฅ ์ด ์ ˆ๋ฐ˜๋ณด๋‹ค ์•ฝ๊ฐ„ ์ž‘์€ ๋ฌธ์ž , ํ™•๋ฅ ์ด ์ ˆ๋ฐ˜๋ณด๋‹ค ์•ฝ๊ฐ„ ์ž‘์€ ๋ฌธ์ž T, A1/1000 = 0.1 %์˜ ํ™•๋ฅ ์„ ๊ฐ–๋Š” ๋ฌธ์ž (๋…๋ฆฝ์ ์œผ๋กœ) ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฌธ์ž๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์–ด์จŒ๋“  A์ด์ „์— ๋ณด์ง€ ๋ชปํ•œ ์บ๋ฆญํ„ฐ๊ฐ€ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ๋ณด์ด๋Š” ๊ฒฝ์šฐ์™€ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์šฐ๋ฆฌ๊ฐ€ L 0 ์ฒด์ œ์—์„œ์ด ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ๋‹ต๋ณ€๋“ค ๋Œ€๋ถ€๋ถ„์ด ์•„๋‹Œ ๋Œ€๋ถ€๋ถ„์„ ์šด์˜ํ•œ๋‹ค๋ฉด, Eand ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ†ต์—ญ์‚ฌ์—๊ฒŒ ๋” ๋‚˜์€ ์ „๋žต์€ ์—†์Šต๋‹ˆ๋‹ค T. ํ‰๊ท ์ ์œผ๋กœ ๋ฌธ์ž์˜ ์•ฝ ์ ˆ๋ฐ˜์ด ๋งž์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ E โ‰ˆ N / 2์™€ โ‰ˆ N / 2๋„ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์••์ถ• ์ „๋žต์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ž ๋‹น ํ•˜๋‚˜ ์ด์ƒ์˜ ๋น„ํŠธ๋กœ ์••์ถ• ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. L์€ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๊ณ„์‚ฐ๋˜๋ฏ€๋กœ L โ‰ˆ N / 8์„ ์–ป์œผ๋ฏ€๋กœ ์ด์ „ ์ „๋žต๋ณด๋‹ค ๋‘ ๋ฐฐ ์ข‹์€ strategy N / 4๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค.

์ด ๋ชจ๋ธ์—์„œ ๋ฌธ์ž ๋‹น ํ•˜๋‚˜ ์ด์ƒ์˜ ๋น„ํŠธ๋ฅผ์ด ์†๋„๋กœ ๋‹ฌ์„ฑํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์†Œํ•œ ๊ฒƒ์ด ์•„๋‹ˆ์ง€๋งŒ ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์‚ฐ์ˆ  ์ฝ”๋”ฉ์ž…๋‹ˆ๋‹ค.

์‚ฐ์ˆ  ์ฝ”๋”ฉ

์ผ๋ฐ˜์ ์œผ๋กœ ์•Œ๋ ค์ง„ ๋ฐ”์™€ ๊ฐ™์ด, ์ธ์ฝ”๋”ฉ ์€ ๋น„ํŠธ / ๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ASCII๋Š” ์˜์–ด ํ…์ŠคํŠธ ๋ฐ ๊ด€๋ จ ๋ฌธ์ž์˜ 7 ๋น„ํŠธ / ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์ด๋ฉฐ, ๊ณ ๋ ค์ค‘์ธ ์›๋ณธ Moby Dick ํŒŒ์ผ์˜ ์ธ์ฝ”๋”ฉ์ž…๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅธ ๋ฌธ์ž๋ณด๋‹ค ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ ASCII์™€ ๊ฐ™์€ ๊ณ ์ • ๋„ˆ๋น„ ์ธ์ฝ”๋”ฉ์ด ์ตœ์ ์ด ์•„๋‹™๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ํ—ˆํ”„๋งŒ ์ฝ”๋”ฉ์— ๋„๋‹ฌํ•ฉ๋‹ˆ๋‹ค . ๋ฌธ์ž ๋‹น ์ •์ˆ˜ ๋น„ํŠธ ์ˆ˜์˜ ๊ณ ์ • (ํ”„๋ฆฌํ”ฝ์Šค ํ”„๋ฆฌ) ์ฝ”๋“œ๋ฅผ ์›ํ•˜๋Š” ๊ฒฝ์šฐ์— ์ตœ์ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์‚ฐ์ˆ  ์ฝ”๋”ฉ ์ด ํ›จ์”ฌ ์ข‹์Šต๋‹ˆ๋‹ค. ๋Œ€๋žต์ ์œผ๋กœ ๋งํ•˜๋ฉด, โ€œ๋ถ„์ˆ˜โ€๋น„ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ณด๋ฅผ ์ธ์ฝ”๋”ฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜จ๋ผ์ธ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜์žˆ๋Š” ์‚ฐ์ˆ  ์ฝ”๋”ฉ์— ๋Œ€ํ•œ ๋งŽ์€ ์•ˆ๋‚ด์„œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜จ๋ผ์ธ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค๋กœ ์ธํ•ด ์—ฌ๊ธฐ์—์„œ ์„ธ๋ถ€ ์‚ฌํ•ญ (ํŠนํžˆ ์‹ค์ œ ๊ตฌํ˜„, ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ธก๋ฉด์—์„œ ์•ฝ๊ฐ„ ๊นŒ๋‹ค๋กœ์šธ ์ˆ˜ ์žˆ์Œ)์„ ๊ฑด๋„ˆ ๋›ฐ์ง€ ๋งŒ ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๋ถˆํ‰ํ•˜๋ฉด์ด ์„น์…˜์„ ๋” ์ž์„ธํžˆ ์„ค๋ช… ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•Œ๋ ค์ง„ ์–ธ์–ด ๋ชจ๋ธ์— ์˜ํ•ด ์‹ค์ œ๋กœ ์ƒ์„ฑ ๋œ ํ…์ŠคํŠธ๊ฐ€์žˆ๋Š” ๊ฒฝ์šฐ, ์‚ฐ์ˆ  ์ฝ”๋”ฉ์€ ํ•ด๋‹น ๋ชจ๋ธ์˜ ํ…์ŠคํŠธ๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ตœ์ ์œผ๋กœ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ์˜๋ฏธ์—์„œ ์ด๊ฒƒ์€ ํ•ด๋‹น ๋ชจ๋ธ์˜ ์••์ถ• ๋ฌธ์ œ๋ฅผ โ€œํ•ด๊ฒฐโ€ํ•ฉ๋‹ˆ๋‹ค. (์‹ค์ œ๋กœ ์ฃผ์š” ๋ฌธ์ œ๋Š” ๋ชจ๋ธ์„ ์•Œ์ง€ ๋ชปํ•˜๊ณ  ์ผ๋ถ€ ๋ชจ๋ธ์€ ์ธ๊ฐ„์˜ ํ…์ŠคํŠธ๋ฅผ ๋ชจ๋ธ๋ง ํ•  ๋•Œ ๋‹ค๋ฅธ ๋ชจ๋ธ๋ณด๋‹ค ๋‚ซ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.)์ด ์ฝ˜ํ…Œ์ŠคํŠธ์—์„œ ์˜ค๋ฅ˜๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ด์ „ ์„น์…˜์˜ ์–ธ์–ด๋กœ ์ด ๊ณผ์ œ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์„ ๋งŒ๋“œ๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์‚ฐ์ˆ  ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ธ์–ด ๋ชจ๋ธ์—์„œ โ€œํžŒํŠธโ€ํŒŒ์ผ์„ ์ƒ์„ฑ ํ•œ ๋‹ค์Œ ์‚ฐ์ˆ  ๋””์ฝ”๋”๋ฅผ โ€œํ†ต์—ญ์‚ฌโ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด ๋ณธ์งˆ์ ์œผ๋กœ ์ตœ์ ํ™” ๋œ ์ธ์ฝ”๋”ฉ์—์„œ, ์šฐ๋ฆฌ๋Š” ํ™•๋ฅ  p๋ฅผ ๊ฐ–๋Š” ๋ฌธ์ž์— ๋Œ€ํ•ด -log_2 (p) ๋น„ํŠธ๋ฅผ ์†Œ๋น„ํ•˜๊ฒŒ๋˜๊ณ , ์ธ์ฝ”๋”ฉ์˜ ์ „์ฒด ๋น„ํŠธ ๋ ˆ์ดํŠธ๋Š” Shannon ์—”ํŠธ๋กœํ”ผ ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ™•๋ฅ ์ด 1/2์— ๊ฐ€๊นŒ์šด ๋ฌธ์ž๋Š” ์ธ์ฝ”๋”ฉํ•˜๋Š” ๋ฐ ์•ฝ 1 ๋น„ํŠธ๊ฐ€ ๊ฑธ๋ฆฌ๊ณ , 1/1000์˜ ํ™•๋ฅ ์ด์žˆ๋Š” ๋ฌธ์ž๋Š” ์•ฝ 10 ๋น„ํŠธ๊ฐ€๋ฉ๋‹ˆ๋‹ค (2 ^ 10์€ ๋Œ€๋žต 1000์ด๋ฏ€๋กœ).

๊ทธ๋Ÿฌ๋‚˜์ด ๊ณผ์ œ์— ๋Œ€ํ•œ ์ ์ˆ˜ ๋ฉ”ํŠธ๋ฆญ์€ ์••์ถ•์„ ์ตœ์ ์˜ ์ „๋žต์œผ๋กœ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์ž˜ ์„ ํƒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋” ์งง์€ ํžŒํŠธ ํŒŒ์ผ์„ ์–ป๋Š” ๋ฐ์žˆ์–ด ํŠธ๋ ˆ์ด๋“œ ์˜คํ”„๋กœ ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‹œ๋„ ํ•  ์ˆ˜์žˆ๋Š” ์ „๋žต ์ค‘ ํ•˜๋‚˜๋Š” ๊ฐ„๋‹จํ•œ ๋ถ„๊ธฐ ์ „๋žต์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์‚ฐ์ˆ  ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ ํ•˜์ง€๋งŒ ๋ชจ๋ธ์˜ ํ™•๋ฅ  ๋ถ„ํฌ๊ฐ€ โ€œ๋‚˜์œโ€๊ฒฝ์šฐ ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ๋ฌธ์ž๋ฅผ ์ถ”์ธกํ•˜์—ฌ ์ธ์ฝ”๋”ฉํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค.

์™œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๊นŒ?

์™œ ์šฐ๋ฆฌ๊ฐ€ โ€œ์˜๋„์ ์œผ๋กœโ€์˜ค๋ฅ˜๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ์ง€ ๋™๊ธฐ๋ฅผ ๋ถ€์—ฌํ•˜๊ธฐ ์œ„ํ•ด ์ด์ „์˜ ์˜ˆ๋ฅผ ๋ถ„์„ํ•ด ๋ด…์‹œ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฐ์ˆ  ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ E๋˜๋Š” ์˜ ๊ฒฝ์šฐ ๋Œ€๋žต 1 ๋น„ํŠธ๋ฅผ ์‚ฌ์šฉ Tํ•˜์ง€๋งŒ์˜ ๊ฒฝ์šฐ์—๋Š” ์•ฝ 10 ๋น„ํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค A.

์ „๋ฐ˜์ ์œผ๋กœ, ์ด๊ฒƒ์€ ์„ธ ๊ฐ€์ง€ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์ง€๋งŒ ๋ฌธ์ž ๋‹น ์กฐ๊ธˆ ์ด์ƒ์„ ์†Œ๋น„ํ•˜๋Š” ๊ฝค ์ข‹์€ ์ธ์ฝ”๋”ฉ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ A๋Š” ๊ฐ€๋Šฅ์„ฑ์ด ๊ฑฐ์˜ ์—†์œผ๋ฉฐ ํ•ด๋‹น 10 ๋น„ํŠธ๋ฅผ ๋„ˆ๋ฌด ์ž์ฃผ ์†Œ๋น„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ A? ์˜ ๊ฒฝ์šฐ ๋Œ€์‹  ์˜ค๋ฅ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ข‹์ง€ ์•Š์„๊นŒ์š”? ๊ฒฐ๊ตญ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋ฉ”ํŠธ๋ฆญ์€ 1 ๋ฐ”์ดํŠธ = 8 ๋น„ํŠธ ๊ธธ์ด์˜ ๊ธธ์ด๊ฐ€ 2 ๊ฐœ์˜ ์˜ค๋ฅ˜์™€ ๊ฐ™๋‹ค๊ณ  ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์ž์— 8/2 = 4 ๋น„ํŠธ ์ด์ƒ์„ ์†Œ๋น„ํ•˜๋Š” ๋Œ€์‹  ์˜ค๋ฅ˜๋ฅผ ์„ ํ˜ธ ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ์˜ค๋ฅ˜๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด 1 ๋ฐ”์ดํŠธ ์ด์ƒ์„ ์†Œ๋น„ํ•˜๋ฉด ํ™•์‹คํžˆ ์ฐจ์„ ์ฑ…์œผ๋กœ ๋“ค๋ฆฝ๋‹ˆ๋‹ค!

โ€œ๋˜๊ฐ๊ธฐโ€๋ฉ”์ปค๋‹ˆ์ฆ˜

์ด ์„น์…˜์—์„œ๋Š”์ด ์†”๋ฃจ์…˜์˜ ์ฃผ์š” ์˜๋ฆฌํ•œ ์ธก๋ฉด์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.์ด ๋ฐฉ๋ฒ•์€ ๋น„์šฉ์—†์ด ์ž˜๋ชป๋œ ์ถ”์ธก์„ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜์žˆ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ๋ถ„์„ ํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์—์„œ ๋˜๊ฐ๊ธฐ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ํŠนํžˆ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ํžŒํŠธ ํŒŒ์ผ์—์„œ 1 ๋น„ํŠธ๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค. 0์ด๋ฉด ์ถ”์ธก Eํ•ฉ๋‹ˆ๋‹ค. 1์ด๋ฉด ์ถ”์ธก Tํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์— ํ˜ธ์ถœ ๋  ๋•Œ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๊ฐ€ ๋ฌด์—‡์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ํžŒํŠธ ํŒŒ์ผ์ด ์ œ๋Œ€๋กœ ์„ค์ • ๋˜๋ฉด Eor ์˜ ๊ฒฝ์šฐ T์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ถ”์ธก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์–ด๋–ป A์Šต๋‹ˆ๊นŒ? ๋˜๊ฐ๊ธฐ ๋ฉ”์ปค๋‹ˆ์ฆ˜์˜ ์•„์ด๋””์–ด๋Š” ๋‹จ์ˆœํžˆ ์ฝ”๋“œ A๋ฅผ ์ž‘์„ฑ ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ๋” ์ •ํ™•ํ•˜๊ฒŒ ๋งํ•˜๋ฉด, ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ๋‚˜์ค‘์— ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๊ฐ€์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜๋ฉด A์€์œ  ์ ์œผ๋กœ โ€ ํ…Œ์ดํ”„๋ฅผ ๋˜๊ฐ์Šต๋‹ˆ๋‹ค โ€œ: ์ด์ „์— ์ฝ์€ ๋น„ํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฝ์€ ๋น„ํŠธ๋Š” ์ฝ”๋”ฉ Eํ•˜๊ฑฐ๋‚˜T๊ทธ๋Ÿฌ๋‚˜ ์ง€๊ธˆ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ„๋‹จํ•œ ์˜ˆ์—์„œ ์ด๊ฒƒ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋™์ผํ•œ ๋ฌธ์ž ( ๋˜๋Š” )๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์† ์ถ”์ธก ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค . ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋‹ค๋ฅธ ๋น„ํŠธ๋ฅผ ์ฝ๊ณ  ๊ณ„์† ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.ET

์ด ํžŒํŠธ ํŒŒ์ผ์˜ ์ธ์ฝ”๋”ฉ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  s๋ฅผ ์™„์ „ํžˆ ๋ฌด์‹œํ•˜๋ฉด์„œ ๋ชจ๋“  Es๋ฅผ 0 ๋น„ํŠธ๋กœ, Ts๋ฅผ 1 ๋น„ํŠธ๋กœ ์ „ํ™˜ Aํ•ฉ๋‹ˆ๋‹ค. ์ด์ „ ์„น์…˜์˜ ๋ ๋ถ€๋ถ„์—์žˆ๋Š” ๋ถ„์„์— ๋”ฐ๋ฅด๋ฉด์ด ์ฒด๊ณ„๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ ์‹œํ‚ค์ง€๋งŒ As๋ฅผ ์ธ์ฝ”๋”ฉํ•˜์ง€ ์•Š์œผ๋ฉด ์„œ ์ „์ฒด์ ์œผ๋กœ ์ ์ˆ˜๋ฅผ ์ค„ ์ž…๋‹ˆ๋‹ค. ๋” ์ž‘์€ ํšจ๊ณผ๋กœ ์‹ค์ œ๋กœ ํžŒํŠธ ํŒŒ์ผ์˜ ๊ธธ์ด๋„ ์ ˆ์•ฝ๋ฉ๋‹ˆ๋‹ค. ๋น„ํŠธ E์™€ ๋น„ํŠธ๊ฐ€ T์•„๋‹Œ ๋น„ํŠธ ์™€ ๋น„ํŠธ๋งˆ๋‹ค ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ๋น„ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ ์ž…๋‹ˆ๋‹ค.

์ž‘์€ ์ •๋ฆฌ

์–ธ์ œ ์˜ค๋ฅ˜๋ฅผ ๋‚ด์•ผํ•ฉ๋‹ˆ๊นŒ? ๋ชจ๋ธ์ด ๋‹ค์Œ ์บ๋ฆญํ„ฐ์— ๋Œ€ํ•œ ํ™•๋ฅ  ๋ถ„ํฌ P๋ฅผ ์ œ๊ณตํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž๋ฅผ coded ์™€ not coded์˜ ๋‘ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค . ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๊ฐ€ ์ฝ”๋”ฉ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ โ€œ๋˜๊ฐ๊ธฐโ€๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธธ์ด์— ์ƒ๊ด€์—†์ด ์˜ค๋ฅ˜๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๊ฐ€ ์ฝ”๋”ฉ๋˜๋ฉด ๋‹ค๋ฅธ ๋ถ„ํฌ Q๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฐ์ˆ  ์ฝ”๋”ฉ์œผ๋กœ ์ธ์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์–ด๋–ค ๋ถ„ํฌ Q๋ฅผ ์„ ํƒํ•ด์•ผํ•ฉ๋‹ˆ๊นŒ? ์ฝ”๋”ฉ ๋œ ๋ฌธ์ž๊ฐ€ ์ฝ”๋”ฉ๋˜์ง€ ์•Š์€ ๋ฌธ์ž๋ณด๋‹ค ๋†’์€ ํ™•๋ฅ  (P)์„ ๊ฐ€์ ธ์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋„ˆ๋ฌด ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ถ„ํฌ Q๋Š” ์ฝ”๋“œํ™” ๋œ ๋ฌธ์ž ๋งŒ ํฌํ•จํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ ์šฐ๋ฆฌ๋Š” ๋‹ค๋ฅธ ๊ฒƒ๋“ค์„ ์ฝ”๋”ฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์—”ํŠธ๋กœํ”ผ๋ฅผ โ€œ์ง€์ถœโ€ํ•ด์„œ๋Š” ์•ˆ๋ฉ๋‹ˆ๋‹ค. ํ™•๋ฅ  ๋ถ„ํฌ Q๊ฐ€ ์ฝ”๋”ฉ ๋œ ๋ฌธ์ž์—์„œ P์— ๋น„๋ก€ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์„๋ณด๊ธฐ์—๋Š” ์กฐ๊ธˆ ๊นŒ๋‹ค ๋กญ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ด€์ฐฐ ๊ฒฐ๊ณผ๋ฅผ ์ข…ํ•ฉํ•˜๋ฉด ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ๋ฌธ์ž๋ฅผ ์ฝ”๋”ฉํ•ด์•ผํ•˜์ง€๋งŒ ๊ฐ€๋Šฅ์„ฑ์ด ์ ์€ ๋ฌธ์ž๋Š” ์ฝ”๋”ฉํ•˜์ง€ ์•Š์•„์•ผํ•˜๋ฉฐ Q๋Š” ์ฝ”๋”ฉ ๋œ ๋ฌธ์ž์— ๋Œ€ํ•ด ๋‹จ์ˆœํžˆ P ๋ฐฐ์œจ์ด ์กฐ์ •๋ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ ์ฝ”๋”ฉ ๋ฌธ์ž์— ๋Œ€ํ•ด ์–ด๋–ค โ€œ์ปท์˜คํ”„โ€๋ฅผ ์„ ํƒํ•ด์•ผํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ๋ฉ‹์ง„ ์ •๋ฆฌ๊ฐ€ ์žˆ์Œ์ด ๋ฐํ˜€์กŒ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ฝ”๋“œํ™” ๋œ ๋ฌธ์ž๊ฐ€ ๊ฒฐํ•ฉ ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์ตœ์†Œ 1 / 5.393 ์ธ ํ•œ ๋ฌธ์ž๋ฅผ ์ฝ”๋”ฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ 5.393์œ„์˜ ํ”„๋กœ๊ทธ๋žจ์˜ ๋ ๋ถ€๋ถ„์—์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ์ž„์˜์˜ ์ƒ์ˆ˜ ๋ชจ์–‘์„ โ€œ์„ค๋ช…โ€ํ•ฉ๋‹ˆ๋‹ค . ์ˆซ์ž 1 / 5.393 โ‰ˆ 0.18542๋Š” ๋ฐฉ์ •์‹ -p log (16)-p log p + (1 + p) log (1 + p) = 0์— ๋Œ€ํ•œ ํ•ด ์ž…๋‹ˆ๋‹ค.

์•„๋งˆ๋„์ด ์ ˆ์ฐจ๋ฅผ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ํ•ฉ๋ฆฌ์ ์ž…๋‹ˆ๋‹ค. ์ด ์Šค ๋‹ˆํŽซ์€ C ++์ž…๋‹ˆ๋‹ค.

// Assume the model is computed elsewhere.
unordered_map<char, double> model;

// Transform p to q
unordered_map<char, double> code;
priority_queue<pair<double,char>> pq;
for( char c : CHARS )
    pq.push( make_pair(model[c], c) );
double s = 0, p;
while( 1 ) {
    char c = pq.top().second;
    pq.pop();
    p = model[c];
    if( s > 5.393*p )
        break;
    code[c] = p;
    s += p;
}
for( auto& kv : code ) {
    char c = kv.first;
    code[c] /= s;
}

ํ•จ๊ป˜ ๋ชจ์•„์„œ

์ด์ „ ์„น์…˜์€ ๋ถˆํ–‰ํžˆ๋„ ์•ฝ๊ฐ„ ๊ธฐ์ˆ ์  ์ธ ๋ถ€๋ถ„์ด์ง€๋งŒ ๋‹ค๋ฅธ ๋ถ€๋ถ„์„ ๋ชจ๋‘ ํ•ฉํ•˜๋ฉด ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์ด ์ฃผ์–ด์ง„ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž ๋‹ค์Œ์— ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก ์š”์ฒญ ๋  ๋•Œ๋งˆ๋‹ค :

  1. Moby Dick์˜ ์•Œ๋ ค์ง„ ์˜ฌ๋ฐ”๋ฅธ ์ ‘๋‘์‚ฌ์— ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค.
  2. ํ…์ŠคํŠธ์˜ (Markov) ๋ชจ๋ธ์„ ์—…๋ฐ์ดํŠธํ•˜์‹ญ์‹œ์˜ค.
  3. ๋น„๋ฐ€ ์†Œ์Šค : ์ด์ „ ์ถ”์ธก์ด ์ •ํ™•ํ•˜์ง€ ์•Š์œผ๋ฉด, ๋˜๊ฐ๊ธฐ ์ด์ „ ์ถ”์ธกํ•˜๊ธฐ ์ „์— ์ƒํƒœ๋กœ ์‚ฐ์ˆ  ๋””์ฝ”๋”์˜ ์ƒํƒœ๋ฅผ!
  4. ๋‹ค์Œ ์บ๋ฆญํ„ฐ์— ๋Œ€ํ•œ ํ™•๋ฅ  ๋ถ„ํฌ P๋ฅผ ์˜ˆ์ธกํ•˜๋„๋ก Markov ๋ชจ๋ธ์— ์š”์ฒญํ•˜์‹ญ์‹œ์˜ค.
  5. ์ด์ „ ์„น์…˜์˜ ์„œ๋ธŒ ๋ฃจํ‹ด์„ ์‚ฌ์šฉํ•˜์—ฌ P๋ฅผ Q๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  6. ๋ถ„ํฌ Q์— ๋”ฐ๋ผ ์‚ฐ์ˆ  ๋””์ฝ”๋”์— ํžŒํŠธ ํŒŒ์ผ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์—์„œ ๋ฌธ์ž๋ฅผ ๋””์ฝ”๋”ฉํ•˜๋„๋ก ์š”์ฒญํ•˜์‹ญ์‹œ์˜ค.
  7. ๊ฒฐ๊ณผ ์บ๋ฆญํ„ฐ๋ฅผ ๋งž์ถฐ๋ณด์„ธ์š”.

ํžŒํŠธ ํŒŒ์ผ์˜ ์ธ์ฝ”๋”ฉ๋„ ๋น„์Šทํ•˜๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ”„๋กœ๊ทธ๋žจ์€ ์˜ฌ๋ฐ”๋ฅธ ๋‹ค์Œ ๋ฌธ์ž๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์ด ์ฝ”๋”ฉ๋˜์–ด์•ผํ•˜๋Š” ๋ฌธ์ž๋ผ๋ฉด, ๋ฌผ๋ก  ๊ทธ ์œ„์— ์‚ฐ์ˆ  ์ธ์ฝ”๋”๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ฝ”๋“œํ™”๋˜์ง€ ์•Š์€ ๋ฌธ์ž์ด๋ฉด ์‚ฐ์ˆ  ์ธ์ฝ”๋”์˜ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ™•๋ฅ  ๋ถ„ํฌ, ์—”ํŠธ๋กœํ”ผ, ์••์ถ• ๋ฐ ์‚ฐ์ˆ  ์ฝ”๋”ฉ๊ณผ ๊ฐ™์€ ์ •๋ณด ์ด๋ก ์  ๋ฐฐ๊ฒฝ์„ ์ดํ•ดํ–ˆ์ง€๋งŒ์ด ๊ฒŒ์‹œ๋ฌผ์„ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๊ณ  ์‹œ๋„ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ (์ด๋ก ์ด ์ฐธ์ธ ์ด์œ ๋Š” ์ œ์™ธ) ์•Œ๋ ค ์ฃผ์‹œ๋ฉด ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ์–ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!


๋‹ต๋ณ€

ํŒŒ์ด์ฌ 3, 2 ยท 267 + 510193 = 510727

์˜ˆ์–ธ์ž

def p():
 d={};s=b''
 while 1:
  p={0:1};r=range(len(s)+1)
  for i in r:
   for c,n in d.setdefault(s[:i],{}).items():p[c]=p.get(c,1)*n**b'\1\6\f\36AcWuvY_v`\270~\333~'[i]
  c=yield max(sorted(p),key=p.get)
  for i in r:e=d[s[:i]];e[c]=e.get(c,1)+1
  s=b'%c'%c+s[:15]

์ด๊ฒƒ์€ 0,โ€ฆ, 16 Markov ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜ ๋ฒ ์ด์ง€์•ˆ ์กฐํ•ฉ์„ ์‚ฌ์šฉํ•˜๊ณ  ๊ฐ€์ค‘์น˜๋Š” [1, 6, 12, 30, 65, 99, 87, 117, 118, 89, 95, 118, 96, 184, 126, 219, 126].

๊ฒฐ๊ณผ๋Š” ์ด๋Ÿฌํ•œ ๊ฐ€์ค‘์น˜์˜ ์„ ํƒ์— ๋งค์šฐ ๋ฏผ๊ฐํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๊ฐ ํ›„๋ณด ๋Œ์—ฐ๋ณ€์ด๊ฐ€์žˆ๋Š” โ€œ์ƒ์› ๋‹ค์ˆ˜๊ฒฐโ€์— ๋Œ€ํ•œ ๋‹ต๋ณ€ ์—์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ํ›„๊ธฐ ํ—ˆ์šฉ ์–ธ๋• ๋“ฑ๋ฐ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ค‘์น˜๋ฅผ ์ตœ์ ํ™”ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹จ์ผ ์ค‘๋Ÿ‰์— ๋Œ€ํ•ด ยฑ 1 ์”ฉ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ

with open('whale2.txt', 'rb') as f:
    g = p()
    wrong = 0
    a = next(g)
    for b in f.read():
        wrong += a != b
        a = g.send(b)
    print(wrong)

๋‹ต๋ณ€

Python 3 , 2 * 279 + 592920 = 593478 2 * 250 + 592467 = 592967 2 * 271 + 592084 = 592626 2 * 278 + 592059 = 592615 2 * 285 + 586660 = 587230 2 * 320 + 585161 = 585801 2 * 339 + 585050 = 585728

d=m={}
s=1
w,v='',0
def f(c):
 global w,m,v,s,d
 if w not in m:m[w]={}
 u=m[w];u[c]=c in u and 1+u[c]or 1;v+=1;q=n=' ';w=w*s+c;s=c!=n
 if w in m:_,n=max((m[w][k],k)for k in m[w])
 elif s-1:n=d in'nedtfo'and't'or'a'
 elif'-'==c:n=c
 elif"'"==c:n='s'
 elif'/'<c<':':n='.'
 if v>4*(n!=q)+66:n='\n'
 if s:d=c
 if c<q:w=w[:-1]+q;v=s=0
 return n

์˜จ๋ผ์ธ์œผ๋กœ ์‚ฌ์šฉํ•ด๋ณด์‹ญ์‹œ์˜ค!

์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜. ๋‹จ์–ด ์ˆ˜์ค€์—์„œ ๋ชจ๋ธ์„ ๊ตฌ์ถ•ํ•˜๋ฉด์„œ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. ์ด ๋‹จ์–ด์—์„œ ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ๊ฒƒ ์ค‘์—์„œ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋‹ค์Œ ๋ฌธ์ž๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ๋” ๋งŽ์€ ์ž…๋ ฅ์ด ๋“ค์–ด ์˜ค๋ฉด ํ…์ŠคํŠธ์—์„œ ์ผ๋ฐ˜์ ์ธ ๋‹จ์–ด๋ฅผ ๊ฝค ์ž˜ ๋ฐฐ์šฐ๊ณ  ๋‹ค์Œ ๋‹จ์–ด ๋ฅผ ์‹œ์ž‘ํ•˜๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ž๋ฅผ ๋ฐฐ์›๋‹ˆ๋‹ค .

์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ๊ฒƒ์ด โ€˜Captaiโ€™์ธ ๊ฒฝ์šฐ โ€œnโ€์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค.
  • โ€œ์บกํ‹ดโ€์ด๋ฉด ๊ณต๊ฐ„์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค
  • ๋‹จ์–ด์˜ ์‹œ์ž‘์ด๊ณ  ๋งˆ์ง€๋ง‰ ๋‹จ์–ด๊ฐ€ โ€œ์บกํ‹ดโ€์ธ ๊ฒฝ์šฐ โ€˜Aโ€™๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค.
  • ์ง€๊ธˆ๊นŒ์ง€ ๋‹จ์–ด๊ฐ€ โ€˜Aโ€™์ด๋ฉด โ€˜h'(๊ทธ๋ฆฌ๊ณ  โ€˜aโ€™์™€ โ€˜bโ€™; โ€˜Cโ€™์™€ ์œ ์‚ฌํ•˜๊ฒŒ)๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค.

์ฒ˜์Œ์—๋Š” ์ž˜ํ•˜์ง€ ์•Š์ง€๋งŒ ๊ฒฐ๊ตญ ์‹ค์ œ ๋‹จ์–ด์˜ ํฐ ๋ถ€๋ถ„์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ๋Œ€์ฒด ์˜ต์…˜์€ ๊ณต๋ฐฑ์ด๋ฉฐ, ์•ž์˜ ๋ฌธ์ž๊ฐ€ โ€œnedtfoโ€, ์ˆซ์ž ๋˜๋Š” ํ•˜์ดํ”ˆ ๋˜๋Š” ์•„ํฌ์ŠคํŠธ๋กœํ”ผ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์•„๋‹Œ ํ•œ ๊ณต๋ฐฑ์€ โ€œaโ€์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ 71 ์ž ๋’ค์˜ ์ค„ ๋ฐ”๊ฟˆ ๋˜๋Š” 66 ์ž ๋’ค์˜ ๊ณต๋ฐฑ์ด ์˜ˆ์ƒ๋˜๋Š” ๊ฒฝ์šฐ ์ ๊ทน์ ์œผ๋กœ ์ค„ ๋ฐ”๊ฟˆ์„ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค. ๋‘˜ ๋‹ค ๋ฐ์ดํ„ฐ์— ๋งž๊ฒŒ ์กฐ์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค ( โ€œtโ€๋Š” ๊ณต๋ฐฑ ํ›„์— ํ›จ์”ฌ ํ”ํ•˜์ง€ ๋งŒ ์ด๋ฏธ ์ž์ฃผ ์˜ˆ์ธก๋˜์—ˆ์œผ๋ฏ€๋กœ โ€ a โ€œ๋Š” 6 ๊ฐ€์ง€ ํŠน์ˆ˜ํ•œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋” ๋‚˜์€ ์ถ”์ธก์ž…๋‹ˆ๋‹ค.)

์–ด๋–ค ๋‹จ์–ด ์Œ์ด ํ•จ๊ป˜๋˜์—ˆ๋Š”์ง€ ๋ฐฐ์šฐ๊ณ  ๋งคํ•‘์„ ๋ฏธ๋ฆฌ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€์น˜๊ฐ€์—†๋Š” ๊ฒƒ์œผ๋กœ ๋ฐํ˜€์กŒ์Šต๋‹ˆ๋‹ค.


๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ…์ŠคํŠธ๋กœ ๋๋‚ฉ๋‹ˆ๋‹ค.

nl tneund his    I woi tis tnlost ahet toie tn tant  wod, ihet taptain Ahab ses
 snd t
oeed Sft   aoid thshtego    Io, fhe soie tn tant  tot the soie      ahe sewbtoon
swn tagd  aoths eatmved fhe sewbtoon wor ta  I sfey  aote of totsonld nive betse
d ahe
hate Whale iorst  Ihe e ioi beaos! -there soi beaos! -there soi beaos!

์ด๊ฒƒ์€ ์ž…๋ ฅ ์˜์ด ๋ถ€๋ถ„์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

๊ทธ์˜ ์ฃผ์œ„์—.

ํƒ€์Šˆ ํ…Œ๊ณ  ๋Œ€๋ณ€์ธ์€โ€œ์•„๋ธŒ ์„ ์žฅ๊ณผ ๊ฑฐ์˜ ๊ฐ™์€ ์ˆœ๊ฐ„์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

โ€œ๋™์ผํ•œ ์ˆœ๊ฐ„์ด ์•„๋‹ˆ์—ˆ๋‹ค. ๋™์ผํ•˜์ง€ ์•Š๋‹ค.-์•„๋‹ˆ, doubloon์€ ๋‚˜์˜ ๊ฒƒ์ด๋‹ค. Fate๋Š” ๋‚˜๋ฅผ ์œ„ํ•ด doubloon์„ ์˜ˆ์•ฝํ–ˆ๋‹ค. ๋‚˜๋Š” ์˜ค์ง ์–ด๋Š ๋ˆ„๊ตฌ๋„ ํฐ ๊ณ ๋ž˜๋ฅผ ๋จผ์ € ์˜ฌ๋ฆด ์ˆ˜ ์—†์—ˆ๋‹ค. -๊ทธ๋…€๊ฐ€ ๋ถˆ์—ˆ๋‹ค!

์ ์ ˆํ•œ ๋ช…์‚ฌ๊ฐ€ ์–ด๋””์—์„œ ๋‚˜์˜ค๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ ๋‹จ์–ด์˜ ๋์ด ๋Œ€๋ถ€๋ถ„ ์˜ณ์Šต๋‹ˆ๋‹ค. โ€œdouโ€์ด ํ‘œ์‹œ๋˜๋ฉด โ€œdoubtโ€๊ฐ€ ํ•„์š”ํ•˜์ง€๋งŒ โ€œlโ€์ด ํ‘œ์‹œ๋˜๋ฉด โ€œdoubloonโ€์ด๋ฉ๋‹ˆ๋‹ค.

๋™์ผํ•œ ๋ชจ๋ธ๋กœ ๋‘ ๋ฒˆ์งธ๋กœ ์‹คํ–‰ํ•˜๋ฉด ๋ฐฉ๊ธˆ ๊ตฌ์ถ• ํ•œ ๋˜ ๋‹ค๋ฅธ 92k ์ •ํ™•๋„ (51.7 %-> 59.3 %)๊ฐ€ ๋ฐœ์ƒํ•˜์ง€๋งŒ ๋‘ ๋ฒˆ์งธ ๋ฐ˜๋ณต์—์„œ ํ•ญ์ƒ 60 % ๋ฏธ๋งŒ์ž…๋‹ˆ๋‹ค.


์ธก์ • ์ฝ”๋“œ๋Š” TIO ๋งํฌ์— ์žˆ๊ฑฐ๋‚˜ ์•ฝ๊ฐ„ ๋” ๋‚˜์€ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค.

total = 0
right = 0
with open('whale.txt') as fp:
    with open('guess.txt', 'w') as dest:
        for l in fp.readlines():
            for c in l:
                last = c
                if p == c: right += 1
                n = f(c)
                p = n
                total += 1
                dest.write(n)
                if total % 10000 == 0:
                    print('{} / {} E={}\r'.format(right, total, total-right), end='')
print('{} / {}: E={}'.format(right, total, total - right))

guess.txt ๋์— ์ถ”์ธก ๋œ ์ถœ๋ ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

C ++, ์ ์ˆ˜ : 2 * 132 + 865821 = 866085

217 ๋ฐ”์ดํŠธ๋ฅผ ์ ˆ์•ฝ ํ•œ @Quentin์—๊ฒŒ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

int f(int c){return c-10?"t \n 2  sS \n  -  08........       huaoRooe oioaoheu thpih eEA \n   neo    enueee neue hteht e"[c-32]:10;}

๋ฌธ์ž๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ž…๋ ฅ ๋ฌธ์ž ๋‹ค์Œ์— ๊ฐ€์žฅ ์ž์ฃผ ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•œ ์†”๋ฃจ์…˜์ž…๋‹ˆ๋‹ค.

๋‹ค์Œ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ ์ˆ˜๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.

#include <iostream>
#include <fstream>

int f(int c);

int main()
{
    std::ifstream file;
    file.open("whale2.txt");

    if (!file.is_open())
        return 1;

    char p_ch, ch;
    file >> std::noskipws >> p_ch;
    int incorrect = 0;
    while (file >> std::noskipws >> ch)
    {
        if (f(p_ch) != ch)
            ++incorrect;
        p_ch = ch;
    }

    file.close();

    std::cout << incorrect;
}

ํŽธ์ง‘ :๋ฅผ ์‚ฌ์šฉ whale2.txtํ•˜๋ฉด ์ ์ˆ˜๊ฐ€ ๋†’์•„์ง‘๋‹ˆ๋‹ค.


๋‹ต๋ณ€

ํŒŒ์ด์ฌ, 2 * 516 + 521122 = 522154

์—ฐ์‚ฐ:

๋˜ ๋‹ค๋ฅธ ํŒŒ์ด์ฌ ์ œ์ถœ,์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ธธ์ด 1, โ€ฆ, l์˜ ์‹œํ€€์Šค๋ฅผ๋ณด๊ณ  ๊ฐ€์žฅ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. ํ™•๋ฅ ์˜ ํ•ฉ์ด ์‚ฌ์šฉ๋˜๋ฉฐ ๋” ๋‚˜์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ํŠธ๋ฆญ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

from collections import Counter as C, defaultdict as D
R,l=range,10
s,n='',[D(C) for _ in R(l+1)]
def A(c):
 global s;s+=c;
 if len(s)<=l:return ' '
 P=D(lambda:0)
 for L in R(1,l+1):
  w=''.join(s[-L-1:-1]);n[L][w].update([c]);w=''.join(s[-L:])
  try:
   q,z=n[L][w].most_common(1)[0];x=sum(list(n[L][w].values()))
  except IndexError:continue
  p=z/x
  if x<3:p*=1/(3-x)
  P[q]+=p
 if not P:return ' '
 return max(P.items(),key=lambda i:i[1])[0]
import this, codecs as d
[A(c) for c in d.decode(this.s, 'rot-13')]

๊ฒฐ๊ณผ :

๋Œ€๋ถ€๋ถ„์˜ ํšก์„ค์ˆ˜์„ค์ด์ง€๋งŒ โ€œFather Mappleโ€๊ณผ ๊ฐ™์€ ๊ฐ„ํ˜น ๋ฌธ๊ตฌ๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.

errors: 521122
TRAINING:
result:  tetlsnowleof the won -opes  aIther Mapple,woneltnsinkeap hsd   lnd the  thth a shoey,aeidorsbine ao
actual: ntal knobs of the man-ropes, Father Mapple cast a look upwards, and then with a truly sailor-like bu
FINAL:
result: mnd wnd round  ahe   ind tveryaonsracting th ards the sol ens-ike aeock tolblescn the sgis of thet t
actual: und and round, then, and ever contracting towards the button-like black bubble at the axis of that s

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ :

๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ์˜ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ๋‹ค๋ฅธ ์ง€์ ์—์„œ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. whale2.txt๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ค„ ๋ฐ”๊ฟˆ์„ ๊ณ„์‚ฐํ•˜๋Š” ์ถ”๊ฐ€ ๋…ผ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

from minified import A

def score(predict, text):
    errors = 0
    newtext = []
    for i, (actual, current) in  enumerate(zip(text[1:], text[:-1])):
        next = predict(current)
        errors += (actual != next)
        newtext.append(next)
        if (i % (len(text) // 100) == 0):
            print ('.', end='', flush=True)
    return errors, ''.join(newtext)

t = open('whale2.txt')
text = t.read()
err2, text2 = score(A, text)
print('errors:', err2)
print("TRAINING:")
print(text2[100000:100100].replace('\n', '\\n'))
print(text1[100001:100101].replace('\n', '\\n'))
print("FINAL:")
print(text2[121400:1215500].replace('\n', '\\n'))
print(text[121401:1215501].replace('\n', '\\n'))