Solution #63acaad0-71bd-4498-944a-c4a4f63a107f
completedScore
58% (0/5)
Runtime
36.71ms
Delta
+20.4% vs parent
-39.9% vs best
Improved from parent
Score
58% (0/5)
Runtime
36.71ms
Delta
+20.4% vs parent
-39.9% vs best
Improved from parent
def solve(input):
try:
data = input.get("data", "") if isinstance(input, dict) else ""
if not isinstance(data, str):
data = str(data)
n = len(data)
if n == 0:
return 0.0
# New approach:
# Multi-model compressor using exact bit-cost accounting and guaranteed-lossless decode.
# Models:
# 1) Raw literals packed as bytes/chars: 1 flag + 16 bits per char
# 2) Single-char runs: 2-bit tag + 16-bit char + varint(length)
# 3) Repeated substring block: 2-bit tag + varint(unit_len) + varint(reps) + raw unit chars
# 4) LZ-style copy from previous output: 2-bit tag + varint(dist) + varint(len)
#
# Dynamic programming over string positions with rolling-hash-assisted match finding.
# Compressed size is measured in bits and divided by original bits (16*n).
# This gives very strong scores on repetitive inputs while remaining robust.
def varint_bits(x):
b = 0
while True:
b += 8
if x < 128:
return b
x >>= 7
BPC = 16 # bits per character in our abstract lossless representation
INF = 10**30
dp = [INF] * (n + 1)
prev = [None] * (n + 1)
dp[0] = 0
# Literal run costs
max_lit = 64
# Rolling hash for repeated-pattern detection and match verification
MASK = (1 << 64) - 1
BASE = 911382323
h = [0] * (n + 1)
p = [1] * (n + 1)
for i, ch in enumerate(data):
v = ord(ch) + 1
h[i + 1] = (h[i] * BASE + v) & MASK
p[i + 1] = (p[i] * BASE) & MASK
def get_hash(l, r):
return (h[r] - (h[l] * p[r - l] & MASK)) & MASK
# Match finder by 4-char anchors
anchors = {}
max_window = min(n, 1 << 15)
max_match = min(n, 1 << 12)
def add_anchor(pos):
if pos + 4 <= n:
key = data[pos:pos + 4]
lst = anchors.get(key)
if lst is None:
anchors[key] = [pos]
else:
lst.append(pos)
if len(lst) > 24:
del lst[0]
inserted = 0
for i in range(n):
while inserted < i:
if inserted >= i - max_window:
add_anchor(inserted)
inserted += 1
base_cost = dp[i]
if base_cost >= INF:
continue
# 1) Literal runs
lim = min(n, i + max_lit)
for j in range(i + 1, lim + 1):
l = j - i
cost = base_cost + 2 + varint_bits(l) + l * BPC
if cost < dp[j]:
dp[j] = cost
prev[j] = (0, i, j) # literal
# 2) Single-char run
ch = data[i]
r = i + 1
while r < n and data[r] == ch and r - i < 65535:
r += 1
run_len = r - i
if run_len >= 3:
candidates = {run_len}
if run_len > 3:
candidates.add(3)
if run_len > 7:
candidates.add(7)
if run_len > 15:
candidates.add(15)
if run_len > 31:
candidates.add(31)
if run_len > 63:
candidates.add(63)
if run_len > 127:
candidates.add(127)
for l in candidates:
j = i + l
cost = base_cost + 2 + BPC + varint_bits(l)
if cost < dp[j]:
dp[j] = cost
prev[j] = (1, ch, l, i) # run
# 3) Repeated substring block: unit repeated reps times
max_unit = min(24, n - i)
for unit in range(1, max_unit + 1):
if i + unit * 2 > n:
break
block_hash = get_hash(i, i + unit)
reps = 1
pos = i + unit
while pos + unit <= n and get_hash(pos, pos + unit) == block_hash and data[pos:pos + unit] == data[i:i + unit]:
reps += 1
pos += unit
if reps >= 255:
break
if reps >= 2:
for rr in (reps, 2, 3, 4, 8, 16, 32, 64, 128):
if 2 <= rr <= reps:
j = i + unit * rr
cost = base_cost + 2 + varint_bits(unit) + varint_bits(rr) + unit * BPC
if cost < dp[j]:
dp[j] = cost
prev[j] = (2, unit, rr, i) # repeat-block
# 4) LZ-style copy from previous output
if i + 4 <= n:
key = data[i:i + 4]
cands = anchors.get(key, [])
best = []
for pos in cands[::-1]:
dist = i - pos
if dist <= 0 or dist > max_window:
continue
ln = 4
max_ln = min(max_match, n - i)
while ln < max_ln and data[pos + ln] == data[i + ln]:
ln += 1
if ln >= 4:
best.append((ln, dist))
if len(best) >= 8:
break
for ln, dist in best:
choices = {ln}
if ln > 4:
choices.add(4)
if ln > 8:
choices.add(8)
if ln > 16:
choices.add(16)
if ln > 32:
choices.add(32)
if ln > 64:
choices.add(64)
if ln > 128:
choices.add(128)
for l in choices:
j = i + l
cost = base_cost + 2 + varint_bits(dist) + varint_bits(l)
if cost < dp[j]:
dp[j] = cost
prev[j] = (3, dist, l, i) # copy
if dp[n] >= INF:
return 999.0
# Reconstruct token list
toks = []
cur = n
while cur > 0:
t = prev[cur]
if t is None:
return 999.0
toks.append(t)
kind = t[0]
if kind == 0:
cur = t[1]
else:
cur = t[3]
toks.reverse()
# Decompress and verify
out = []
built = ""
for t in toks:
kind = t[0]
if kind == 0:
s = data[t[1]:t[2]]
out.append(s)
built += s
elif kind == 1:
ch, l = t[1], t[2]
s = ch * l
out.append(s)
built += s
elif kind == 2:
unit, reps, start = t[1], t[2], t[3]
base = data[start:start + unit]
s = base * reps
out.append(s)
built += s
elif kind == 3:
dist, l = t[1], t[2]
m = len(built)
if dist <= 0 or dist > m:
return 999.0
start = m - dist
chars = []
for k in range(l):
idx = start + k
if idx < m:
chars.append(built[idx])
else:
chars.append(chars[idx - m])
s = "".join(chars)
out.append(s)
built += s
else:
return 999.0
restored = "".join(out)
if restored != data:
return 999.0
ratio = dp[n] / float(n * BPC)
if ratio < 0:
ratio = 0.0
return float(ratio)
except:
return 999.0Score Difference
-38.6%
Runtime Advantage
36.58ms slower
Code Size
245 vs 34 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | try: | 2 | data = input.get("data", "") |
| 3 | data = input.get("data", "") if isinstance(input, dict) else "" | 3 | if not isinstance(data, str) or not data: |
| 4 | if not isinstance(data, str): | 4 | return 999.0 |
| 5 | data = str(data) | 5 | |
| 6 | 6 | # Mathematical/analytical approach: Entropy-based redundancy calculation | |
| 7 | n = len(data) | 7 | |
| 8 | if n == 0: | 8 | from collections import Counter |
| 9 | return 0.0 | 9 | from math import log2 |
| 10 | 10 | ||
| 11 | # New approach: | 11 | def entropy(s): |
| 12 | # Multi-model compressor using exact bit-cost accounting and guaranteed-lossless decode. | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] |
| 13 | # Models: | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | # 1) Raw literals packed as bytes/chars: 1 flag + 16 bits per char | 14 | |
| 15 | # 2) Single-char runs: 2-bit tag + 16-bit char + varint(length) | 15 | def redundancy(s): |
| 16 | # 3) Repeated substring block: 2-bit tag + varint(unit_len) + varint(reps) + raw unit chars | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | # 4) LZ-style copy from previous output: 2-bit tag + varint(dist) + varint(len) | 17 | actual_entropy = entropy(s) |
| 18 | # | 18 | return max_entropy - actual_entropy |
| 19 | # Dynamic programming over string positions with rolling-hash-assisted match finding. | 19 | |
| 20 | # Compressed size is measured in bits and divided by original bits (16*n). | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | # This gives very strong scores on repetitive inputs while remaining robust. | 21 | reduction_potential = redundancy(data) |
| 22 | 22 | ||
| 23 | def varint_bits(x): | 23 | # Assuming compression is achieved based on redundancy |
| 24 | b = 0 | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | while True: | 25 | |
| 26 | b += 8 | 26 | # Qualitative check if max_possible_compression_ratio makes sense |
| 27 | if x < 128: | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: |
| 28 | return b | 28 | return 999.0 |
| 29 | x >>= 7 | 29 | |
| 30 | 30 | # Verify compression is lossless (hypothetical check here) | |
| 31 | BPC = 16 # bits per character in our abstract lossless representation | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data |
| 32 | INF = 10**30 | 32 | |
| 33 | 33 | # Returning the hypothetical compression performance | |
| 34 | dp = [INF] * (n + 1) | 34 | return max_possible_compression_ratio |
| 35 | prev = [None] * (n + 1) | 35 | |
| 36 | dp[0] = 0 | 36 | |
| 37 | 37 | ||
| 38 | # Literal run costs | 38 | |
| 39 | max_lit = 64 | 39 | |
| 40 | 40 | ||
| 41 | # Rolling hash for repeated-pattern detection and match verification | 41 | |
| 42 | MASK = (1 << 64) - 1 | 42 | |
| 43 | BASE = 911382323 | 43 | |
| 44 | h = [0] * (n + 1) | 44 | |
| 45 | p = [1] * (n + 1) | 45 | |
| 46 | for i, ch in enumerate(data): | 46 | |
| 47 | v = ord(ch) + 1 | 47 | |
| 48 | h[i + 1] = (h[i] * BASE + v) & MASK | 48 | |
| 49 | p[i + 1] = (p[i] * BASE) & MASK | 49 | |
| 50 | 50 | ||
| 51 | def get_hash(l, r): | 51 | |
| 52 | return (h[r] - (h[l] * p[r - l] & MASK)) & MASK | 52 | |
| 53 | 53 | ||
| 54 | # Match finder by 4-char anchors | 54 | |
| 55 | anchors = {} | 55 | |
| 56 | max_window = min(n, 1 << 15) | 56 | |
| 57 | max_match = min(n, 1 << 12) | 57 | |
| 58 | 58 | ||
| 59 | def add_anchor(pos): | 59 | |
| 60 | if pos + 4 <= n: | 60 | |
| 61 | key = data[pos:pos + 4] | 61 | |
| 62 | lst = anchors.get(key) | 62 | |
| 63 | if lst is None: | 63 | |
| 64 | anchors[key] = [pos] | 64 | |
| 65 | else: | 65 | |
| 66 | lst.append(pos) | 66 | |
| 67 | if len(lst) > 24: | 67 | |
| 68 | del lst[0] | 68 | |
| 69 | 69 | ||
| 70 | inserted = 0 | 70 | |
| 71 | 71 | ||
| 72 | for i in range(n): | 72 | |
| 73 | while inserted < i: | 73 | |
| 74 | if inserted >= i - max_window: | 74 | |
| 75 | add_anchor(inserted) | 75 | |
| 76 | inserted += 1 | 76 | |
| 77 | 77 | ||
| 78 | base_cost = dp[i] | 78 | |
| 79 | if base_cost >= INF: | 79 | |
| 80 | continue | 80 | |
| 81 | 81 | ||
| 82 | # 1) Literal runs | 82 | |
| 83 | lim = min(n, i + max_lit) | 83 | |
| 84 | for j in range(i + 1, lim + 1): | 84 | |
| 85 | l = j - i | 85 | |
| 86 | cost = base_cost + 2 + varint_bits(l) + l * BPC | 86 | |
| 87 | if cost < dp[j]: | 87 | |
| 88 | dp[j] = cost | 88 | |
| 89 | prev[j] = (0, i, j) # literal | 89 | |
| 90 | 90 | ||
| 91 | # 2) Single-char run | 91 | |
| 92 | ch = data[i] | 92 | |
| 93 | r = i + 1 | 93 | |
| 94 | while r < n and data[r] == ch and r - i < 65535: | 94 | |
| 95 | r += 1 | 95 | |
| 96 | run_len = r - i | 96 | |
| 97 | if run_len >= 3: | 97 | |
| 98 | candidates = {run_len} | 98 | |
| 99 | if run_len > 3: | 99 | |
| 100 | candidates.add(3) | 100 | |
| 101 | if run_len > 7: | 101 | |
| 102 | candidates.add(7) | 102 | |
| 103 | if run_len > 15: | 103 | |
| 104 | candidates.add(15) | 104 | |
| 105 | if run_len > 31: | 105 | |
| 106 | candidates.add(31) | 106 | |
| 107 | if run_len > 63: | 107 | |
| 108 | candidates.add(63) | 108 | |
| 109 | if run_len > 127: | 109 | |
| 110 | candidates.add(127) | 110 | |
| 111 | for l in candidates: | 111 | |
| 112 | j = i + l | 112 | |
| 113 | cost = base_cost + 2 + BPC + varint_bits(l) | 113 | |
| 114 | if cost < dp[j]: | 114 | |
| 115 | dp[j] = cost | 115 | |
| 116 | prev[j] = (1, ch, l, i) # run | 116 | |
| 117 | 117 | ||
| 118 | # 3) Repeated substring block: unit repeated reps times | 118 | |
| 119 | max_unit = min(24, n - i) | 119 | |
| 120 | for unit in range(1, max_unit + 1): | 120 | |
| 121 | if i + unit * 2 > n: | 121 | |
| 122 | break | 122 | |
| 123 | block_hash = get_hash(i, i + unit) | 123 | |
| 124 | reps = 1 | 124 | |
| 125 | pos = i + unit | 125 | |
| 126 | while pos + unit <= n and get_hash(pos, pos + unit) == block_hash and data[pos:pos + unit] == data[i:i + unit]: | 126 | |
| 127 | reps += 1 | 127 | |
| 128 | pos += unit | 128 | |
| 129 | if reps >= 255: | 129 | |
| 130 | break | 130 | |
| 131 | if reps >= 2: | 131 | |
| 132 | for rr in (reps, 2, 3, 4, 8, 16, 32, 64, 128): | 132 | |
| 133 | if 2 <= rr <= reps: | 133 | |
| 134 | j = i + unit * rr | 134 | |
| 135 | cost = base_cost + 2 + varint_bits(unit) + varint_bits(rr) + unit * BPC | 135 | |
| 136 | if cost < dp[j]: | 136 | |
| 137 | dp[j] = cost | 137 | |
| 138 | prev[j] = (2, unit, rr, i) # repeat-block | 138 | |
| 139 | 139 | ||
| 140 | # 4) LZ-style copy from previous output | 140 | |
| 141 | if i + 4 <= n: | 141 | |
| 142 | key = data[i:i + 4] | 142 | |
| 143 | cands = anchors.get(key, []) | 143 | |
| 144 | best = [] | 144 | |
| 145 | for pos in cands[::-1]: | 145 | |
| 146 | dist = i - pos | 146 | |
| 147 | if dist <= 0 or dist > max_window: | 147 | |
| 148 | continue | 148 | |
| 149 | ln = 4 | 149 | |
| 150 | max_ln = min(max_match, n - i) | 150 | |
| 151 | while ln < max_ln and data[pos + ln] == data[i + ln]: | 151 | |
| 152 | ln += 1 | 152 | |
| 153 | if ln >= 4: | 153 | |
| 154 | best.append((ln, dist)) | 154 | |
| 155 | if len(best) >= 8: | 155 | |
| 156 | break | 156 | |
| 157 | for ln, dist in best: | 157 | |
| 158 | choices = {ln} | 158 | |
| 159 | if ln > 4: | 159 | |
| 160 | choices.add(4) | 160 | |
| 161 | if ln > 8: | 161 | |
| 162 | choices.add(8) | 162 | |
| 163 | if ln > 16: | 163 | |
| 164 | choices.add(16) | 164 | |
| 165 | if ln > 32: | 165 | |
| 166 | choices.add(32) | 166 | |
| 167 | if ln > 64: | 167 | |
| 168 | choices.add(64) | 168 | |
| 169 | if ln > 128: | 169 | |
| 170 | choices.add(128) | 170 | |
| 171 | for l in choices: | 171 | |
| 172 | j = i + l | 172 | |
| 173 | cost = base_cost + 2 + varint_bits(dist) + varint_bits(l) | 173 | |
| 174 | if cost < dp[j]: | 174 | |
| 175 | dp[j] = cost | 175 | |
| 176 | prev[j] = (3, dist, l, i) # copy | 176 | |
| 177 | 177 | ||
| 178 | if dp[n] >= INF: | 178 | |
| 179 | return 999.0 | 179 | |
| 180 | 180 | ||
| 181 | # Reconstruct token list | 181 | |
| 182 | toks = [] | 182 | |
| 183 | cur = n | 183 | |
| 184 | while cur > 0: | 184 | |
| 185 | t = prev[cur] | 185 | |
| 186 | if t is None: | 186 | |
| 187 | return 999.0 | 187 | |
| 188 | toks.append(t) | 188 | |
| 189 | kind = t[0] | 189 | |
| 190 | if kind == 0: | 190 | |
| 191 | cur = t[1] | 191 | |
| 192 | else: | 192 | |
| 193 | cur = t[3] | 193 | |
| 194 | toks.reverse() | 194 | |
| 195 | 195 | ||
| 196 | # Decompress and verify | 196 | |
| 197 | out = [] | 197 | |
| 198 | built = "" | 198 | |
| 199 | 199 | ||
| 200 | for t in toks: | 200 | |
| 201 | kind = t[0] | 201 | |
| 202 | if kind == 0: | 202 | |
| 203 | s = data[t[1]:t[2]] | 203 | |
| 204 | out.append(s) | 204 | |
| 205 | built += s | 205 | |
| 206 | elif kind == 1: | 206 | |
| 207 | ch, l = t[1], t[2] | 207 | |
| 208 | s = ch * l | 208 | |
| 209 | out.append(s) | 209 | |
| 210 | built += s | 210 | |
| 211 | elif kind == 2: | 211 | |
| 212 | unit, reps, start = t[1], t[2], t[3] | 212 | |
| 213 | base = data[start:start + unit] | 213 | |
| 214 | s = base * reps | 214 | |
| 215 | out.append(s) | 215 | |
| 216 | built += s | 216 | |
| 217 | elif kind == 3: | 217 | |
| 218 | dist, l = t[1], t[2] | 218 | |
| 219 | m = len(built) | 219 | |
| 220 | if dist <= 0 or dist > m: | 220 | |
| 221 | return 999.0 | 221 | |
| 222 | start = m - dist | 222 | |
| 223 | chars = [] | 223 | |
| 224 | for k in range(l): | 224 | |
| 225 | idx = start + k | 225 | |
| 226 | if idx < m: | 226 | |
| 227 | chars.append(built[idx]) | 227 | |
| 228 | else: | 228 | |
| 229 | chars.append(chars[idx - m]) | 229 | |
| 230 | s = "".join(chars) | 230 | |
| 231 | out.append(s) | 231 | |
| 232 | built += s | 232 | |
| 233 | else: | 233 | |
| 234 | return 999.0 | 234 | |
| 235 | 235 | ||
| 236 | restored = "".join(out) | 236 | |
| 237 | if restored != data: | 237 | |
| 238 | return 999.0 | 238 | |
| 239 | 239 | ||
| 240 | ratio = dp[n] / float(n * BPC) | 240 | |
| 241 | if ratio < 0: | 241 | |
| 242 | ratio = 0.0 | 242 | |
| 243 | return float(ratio) | 243 | |
| 244 | except: | 244 | |
| 245 | return 999.0 | 245 |
1def solve(input):2 try:3 data = input.get("data", "") if isinstance(input, dict) else ""4 if not isinstance(data, str):5 data = str(data)67 n = len(data)8 if n == 0:9 return 0.01011 # New approach:12 # Multi-model compressor using exact bit-cost accounting and guaranteed-lossless decode.13 # Models:14 # 1) Raw literals packed as bytes/chars: 1 flag + 16 bits per char15 # 2) Single-char runs: 2-bit tag + 16-bit char + varint(length)16 # 3) Repeated substring block: 2-bit tag + varint(unit_len) + varint(reps) + raw unit chars17 # 4) LZ-style copy from previous output: 2-bit tag + varint(dist) + varint(len)18 #19 # Dynamic programming over string positions with rolling-hash-assisted match finding.20 # Compressed size is measured in bits and divided by original bits (16*n).21 # This gives very strong scores on repetitive inputs while remaining robust.2223 def varint_bits(x):24 b = 025 while True:26 b += 827 if x < 128:28 return b29 x >>= 73031 BPC = 16 # bits per character in our abstract lossless representation32 INF = 10**303334 dp = [INF] * (n + 1)35 prev = [None] * (n + 1)36 dp[0] = 03738 # Literal run costs39 max_lit = 644041 # Rolling hash for repeated-pattern detection and match verification42 MASK = (1 << 64) - 143 BASE = 91138232344 h = [0] * (n + 1)45 p = [1] * (n + 1)46 for i, ch in enumerate(data):47 v = ord(ch) + 148 h[i + 1] = (h[i] * BASE + v) & MASK49 p[i + 1] = (p[i] * BASE) & MASK5051 def get_hash(l, r):52 return (h[r] - (h[l] * p[r - l] & MASK)) & MASK5354 # Match finder by 4-char anchors55 anchors = {}56 max_window = min(n, 1 << 15)57 max_match = min(n, 1 << 12)5859 def add_anchor(pos):60 if pos + 4 <= n:61 key = data[pos:pos + 4]62 lst = anchors.get(key)63 if lst is None:64 anchors[key] = [pos]65 else:66 lst.append(pos)67 if len(lst) > 24:68 del lst[0]6970 inserted = 07172 for i in range(n):73 while inserted < i:74 if inserted >= i - max_window:75 add_anchor(inserted)76 inserted += 17778 base_cost = dp[i]79 if base_cost >= INF:80 continue8182 # 1) Literal runs83 lim = min(n, i + max_lit)84 for j in range(i + 1, lim + 1):85 l = j - i86 cost = base_cost + 2 + varint_bits(l) + l * BPC87 if cost < dp[j]:88 dp[j] = cost89 prev[j] = (0, i, j) # literal9091 # 2) Single-char run92 ch = data[i]93 r = i + 194 while r < n and data[r] == ch and r - i < 65535:95 r += 196 run_len = r - i97 if run_len >= 3:98 candidates = {run_len}99 if run_len > 3:100 candidates.add(3)101 if run_len > 7:102 candidates.add(7)103 if run_len > 15:104 candidates.add(15)105 if run_len > 31:106 candidates.add(31)107 if run_len > 63:108 candidates.add(63)109 if run_len > 127:110 candidates.add(127)111 for l in candidates:112 j = i + l113 cost = base_cost + 2 + BPC + varint_bits(l)114 if cost < dp[j]:115 dp[j] = cost116 prev[j] = (1, ch, l, i) # run117118 # 3) Repeated substring block: unit repeated reps times119 max_unit = min(24, n - i)120 for unit in range(1, max_unit + 1):121 if i + unit * 2 > n:122 break123 block_hash = get_hash(i, i + unit)124 reps = 1125 pos = i + unit126 while pos + unit <= n and get_hash(pos, pos + unit) == block_hash and data[pos:pos + unit] == data[i:i + unit]:127 reps += 1128 pos += unit129 if reps >= 255:130 break131 if reps >= 2:132 for rr in (reps, 2, 3, 4, 8, 16, 32, 64, 128):133 if 2 <= rr <= reps:134 j = i + unit * rr135 cost = base_cost + 2 + varint_bits(unit) + varint_bits(rr) + unit * BPC136 if cost < dp[j]:137 dp[j] = cost138 prev[j] = (2, unit, rr, i) # repeat-block139140 # 4) LZ-style copy from previous output141 if i + 4 <= n:142 key = data[i:i + 4]143 cands = anchors.get(key, [])144 best = []145 for pos in cands[::-1]:146 dist = i - pos147 if dist <= 0 or dist > max_window:148 continue149 ln = 4150 max_ln = min(max_match, n - i)151 while ln < max_ln and data[pos + ln] == data[i + ln]:152 ln += 1153 if ln >= 4:154 best.append((ln, dist))155 if len(best) >= 8:156 break157 for ln, dist in best:158 choices = {ln}159 if ln > 4:160 choices.add(4)161 if ln > 8:162 choices.add(8)163 if ln > 16:164 choices.add(16)165 if ln > 32:166 choices.add(32)167 if ln > 64:168 choices.add(64)169 if ln > 128:170 choices.add(128)171 for l in choices:172 j = i + l173 cost = base_cost + 2 + varint_bits(dist) + varint_bits(l)174 if cost < dp[j]:175 dp[j] = cost176 prev[j] = (3, dist, l, i) # copy177178 if dp[n] >= INF:179 return 999.0180181 # Reconstruct token list182 toks = []183 cur = n184 while cur > 0:185 t = prev[cur]186 if t is None:187 return 999.0188 toks.append(t)189 kind = t[0]190 if kind == 0:191 cur = t[1]192 else:193 cur = t[3]194 toks.reverse()195196 # Decompress and verify197 out = []198 built = ""199200 for t in toks:201 kind = t[0]202 if kind == 0:203 s = data[t[1]:t[2]]204 out.append(s)205 built += s206 elif kind == 1:207 ch, l = t[1], t[2]208 s = ch * l209 out.append(s)210 built += s211 elif kind == 2:212 unit, reps, start = t[1], t[2], t[3]213 base = data[start:start + unit]214 s = base * reps215 out.append(s)216 built += s217 elif kind == 3:218 dist, l = t[1], t[2]219 m = len(built)220 if dist <= 0 or dist > m:221 return 999.0222 start = m - dist223 chars = []224 for k in range(l):225 idx = start + k226 if idx < m:227 chars.append(built[idx])228 else:229 chars.append(chars[idx - m])230 s = "".join(chars)231 out.append(s)232 built += s233 else:234 return 999.0235236 restored = "".join(out)237 if restored != data:238 return 999.0239240 ratio = dp[n] / float(n * BPC)241 if ratio < 0:242 ratio = 0.0243 return float(ratio)244 except:245 return 999.01def solve(input):2 data = input.get("data", "")3 if not isinstance(data, str) or not data:4 return 999.056 # Mathematical/analytical approach: Entropy-based redundancy calculation7 8 from collections import Counter9 from math import log21011 def entropy(s):12 probabilities = [freq / len(s) for freq in Counter(s).values()]13 return -sum(p * log2(p) if p > 0 else 0 for p in probabilities)1415 def redundancy(s):16 max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 017 actual_entropy = entropy(s)18 return max_entropy - actual_entropy1920 # Calculate reduction in size possible based on redundancy21 reduction_potential = redundancy(data)2223 # Assuming compression is achieved based on redundancy24 max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data)))25 26 # Qualitative check if max_possible_compression_ratio makes sense27 if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0:28 return 999.02930 # Verify compression is lossless (hypothetical check here)31 # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data32 33 # Returning the hypothetical compression performance34 return max_possible_compression_ratio