Solution #693a4dda-637f-45c6-8d7b-0708e426e7ea
completedScore
33% (0/5)
Runtime
20.79ms
Delta
-30.8% vs parent
-65.6% vs best
Regression from parent
Score
33% (0/5)
Runtime
20.79ms
Delta
-30.8% vs parent
-65.6% vs best
Regression 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
# Divide-and-conquer style compressor:
# recursively choose among:
# - literal block
# - repeated substring block: k copies of a pattern
# - split into two halves and merge their encodings
#
# Compression size is measured in abstract units.
# Losslessness is verified by decompression.
memo = {}
def divisors(x):
ds = []
d = 1
while d * d <= x:
if x % d == 0:
ds.append(d)
if d * d != x:
ds.append(x // d)
d += 1
ds.sort()
return ds
def enc_cost(node):
t = node[0]
if t == "L":
return 1 + len(node[1])
if t == "C":
return 2 + len(str(node[2])) + enc_cost(node[1])
if t == "S":
return enc_cost(node[1]) + enc_cost(node[2])
return 10**18
def build(s):
if s in memo:
return memo[s]
m = len(s)
best = ("L", s)
best_cost = 1 + m
if m >= 2:
# Try repeated pattern encoding: s = pattern * k
for d in divisors(m):
if d == m:
continue
k = m // d
pat = s[:d]
if pat * k == s:
sub = build(pat)
cand = ("C", sub, k)
c = 2 + len(str(k)) + enc_cost(sub)
if c < best_cost:
best = cand
best_cost = c
# Divide and conquer split/merge
mid = m // 2
split_points = [mid]
if mid - 1 > 0:
split_points.append(mid - 1)
if mid + 1 < m:
split_points.append(mid + 1)
# Add a few content-aware split points at repeated-prefix boundaries
lim = min(m - 1, 16)
for i in range(1, lim + 1):
if s[:i] == s[i:2 * i] and 2 * i <= m:
split_points.append(i)
if s[m - i:] == s[m - 2 * i:m - i] and m - i > 0:
split_points.append(m - i)
seen = set()
for p in split_points:
if p <= 0 or p >= m or p in seen:
continue
seen.add(p)
left = build(s[:p])
right = build(s[p:])
cand = ("S", left, right)
c = enc_cost(left) + enc_cost(right)
if c < best_cost:
best = cand
best_cost = c
memo[s] = best
return best
root = build(data)
def decompress(node):
t = node[0]
if t == "L":
return node[1]
if t == "C":
sub = decompress(node[1])
if sub is None:
return None
return sub * node[2]
if t == "S":
a = decompress(node[1])
if a is None:
return None
b = decompress(node[2])
if b is None:
return None
return a + b
return None
restored = decompress(root)
if restored != data:
return 999.0
ratio = enc_cost(root) / n
return float(ratio)
except:
return 999.0Score Difference
-63.3%
Runtime Advantage
20.66ms slower
Code Size
127 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 | # Divide-and-conquer style compressor: | 11 | def entropy(s): |
| 12 | # recursively choose among: | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] |
| 13 | # - literal block | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | # - repeated substring block: k copies of a pattern | 14 | |
| 15 | # - split into two halves and merge their encodings | 15 | def redundancy(s): |
| 16 | # | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | # Compression size is measured in abstract units. | 17 | actual_entropy = entropy(s) |
| 18 | # Losslessness is verified by decompression. | 18 | return max_entropy - actual_entropy |
| 19 | 19 | ||
| 20 | memo = {} | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | 21 | reduction_potential = redundancy(data) | |
| 22 | def divisors(x): | 22 | |
| 23 | ds = [] | 23 | # Assuming compression is achieved based on redundancy |
| 24 | d = 1 | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | while d * d <= x: | 25 | |
| 26 | if x % d == 0: | 26 | # Qualitative check if max_possible_compression_ratio makes sense |
| 27 | ds.append(d) | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: |
| 28 | if d * d != x: | 28 | return 999.0 |
| 29 | ds.append(x // d) | 29 | |
| 30 | d += 1 | 30 | # Verify compression is lossless (hypothetical check here) |
| 31 | ds.sort() | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data |
| 32 | return ds | 32 | |
| 33 | 33 | # Returning the hypothetical compression performance | |
| 34 | def enc_cost(node): | 34 | return max_possible_compression_ratio |
| 35 | t = node[0] | 35 | |
| 36 | if t == "L": | 36 | |
| 37 | return 1 + len(node[1]) | 37 | |
| 38 | if t == "C": | 38 | |
| 39 | return 2 + len(str(node[2])) + enc_cost(node[1]) | 39 | |
| 40 | if t == "S": | 40 | |
| 41 | return enc_cost(node[1]) + enc_cost(node[2]) | 41 | |
| 42 | return 10**18 | 42 | |
| 43 | 43 | ||
| 44 | def build(s): | 44 | |
| 45 | if s in memo: | 45 | |
| 46 | return memo[s] | 46 | |
| 47 | 47 | ||
| 48 | m = len(s) | 48 | |
| 49 | best = ("L", s) | 49 | |
| 50 | best_cost = 1 + m | 50 | |
| 51 | 51 | ||
| 52 | if m >= 2: | 52 | |
| 53 | # Try repeated pattern encoding: s = pattern * k | 53 | |
| 54 | for d in divisors(m): | 54 | |
| 55 | if d == m: | 55 | |
| 56 | continue | 56 | |
| 57 | k = m // d | 57 | |
| 58 | pat = s[:d] | 58 | |
| 59 | if pat * k == s: | 59 | |
| 60 | sub = build(pat) | 60 | |
| 61 | cand = ("C", sub, k) | 61 | |
| 62 | c = 2 + len(str(k)) + enc_cost(sub) | 62 | |
| 63 | if c < best_cost: | 63 | |
| 64 | best = cand | 64 | |
| 65 | best_cost = c | 65 | |
| 66 | 66 | ||
| 67 | # Divide and conquer split/merge | 67 | |
| 68 | mid = m // 2 | 68 | |
| 69 | split_points = [mid] | 69 | |
| 70 | if mid - 1 > 0: | 70 | |
| 71 | split_points.append(mid - 1) | 71 | |
| 72 | if mid + 1 < m: | 72 | |
| 73 | split_points.append(mid + 1) | 73 | |
| 74 | 74 | ||
| 75 | # Add a few content-aware split points at repeated-prefix boundaries | 75 | |
| 76 | lim = min(m - 1, 16) | 76 | |
| 77 | for i in range(1, lim + 1): | 77 | |
| 78 | if s[:i] == s[i:2 * i] and 2 * i <= m: | 78 | |
| 79 | split_points.append(i) | 79 | |
| 80 | if s[m - i:] == s[m - 2 * i:m - i] and m - i > 0: | 80 | |
| 81 | split_points.append(m - i) | 81 | |
| 82 | 82 | ||
| 83 | seen = set() | 83 | |
| 84 | for p in split_points: | 84 | |
| 85 | if p <= 0 or p >= m or p in seen: | 85 | |
| 86 | continue | 86 | |
| 87 | seen.add(p) | 87 | |
| 88 | left = build(s[:p]) | 88 | |
| 89 | right = build(s[p:]) | 89 | |
| 90 | cand = ("S", left, right) | 90 | |
| 91 | c = enc_cost(left) + enc_cost(right) | 91 | |
| 92 | if c < best_cost: | 92 | |
| 93 | best = cand | 93 | |
| 94 | best_cost = c | 94 | |
| 95 | 95 | ||
| 96 | memo[s] = best | 96 | |
| 97 | return best | 97 | |
| 98 | 98 | ||
| 99 | root = build(data) | 99 | |
| 100 | 100 | ||
| 101 | def decompress(node): | 101 | |
| 102 | t = node[0] | 102 | |
| 103 | if t == "L": | 103 | |
| 104 | return node[1] | 104 | |
| 105 | if t == "C": | 105 | |
| 106 | sub = decompress(node[1]) | 106 | |
| 107 | if sub is None: | 107 | |
| 108 | return None | 108 | |
| 109 | return sub * node[2] | 109 | |
| 110 | if t == "S": | 110 | |
| 111 | a = decompress(node[1]) | 111 | |
| 112 | if a is None: | 112 | |
| 113 | return None | 113 | |
| 114 | b = decompress(node[2]) | 114 | |
| 115 | if b is None: | 115 | |
| 116 | return None | 116 | |
| 117 | return a + b | 117 | |
| 118 | return None | 118 | |
| 119 | 119 | ||
| 120 | restored = decompress(root) | 120 | |
| 121 | if restored != data: | 121 | |
| 122 | return 999.0 | 122 | |
| 123 | 123 | ||
| 124 | ratio = enc_cost(root) / n | 124 | |
| 125 | return float(ratio) | 125 | |
| 126 | except: | 126 | |
| 127 | return 999.0 | 127 |
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 # Divide-and-conquer style compressor:12 # recursively choose among:13 # - literal block14 # - repeated substring block: k copies of a pattern15 # - split into two halves and merge their encodings16 #17 # Compression size is measured in abstract units.18 # Losslessness is verified by decompression.1920 memo = {}2122 def divisors(x):23 ds = []24 d = 125 while d * d <= x:26 if x % d == 0:27 ds.append(d)28 if d * d != x:29 ds.append(x // d)30 d += 131 ds.sort()32 return ds3334 def enc_cost(node):35 t = node[0]36 if t == "L":37 return 1 + len(node[1])38 if t == "C":39 return 2 + len(str(node[2])) + enc_cost(node[1])40 if t == "S":41 return enc_cost(node[1]) + enc_cost(node[2])42 return 10**184344 def build(s):45 if s in memo:46 return memo[s]4748 m = len(s)49 best = ("L", s)50 best_cost = 1 + m5152 if m >= 2:53 # Try repeated pattern encoding: s = pattern * k54 for d in divisors(m):55 if d == m:56 continue57 k = m // d58 pat = s[:d]59 if pat * k == s:60 sub = build(pat)61 cand = ("C", sub, k)62 c = 2 + len(str(k)) + enc_cost(sub)63 if c < best_cost:64 best = cand65 best_cost = c6667 # Divide and conquer split/merge68 mid = m // 269 split_points = [mid]70 if mid - 1 > 0:71 split_points.append(mid - 1)72 if mid + 1 < m:73 split_points.append(mid + 1)7475 # Add a few content-aware split points at repeated-prefix boundaries76 lim = min(m - 1, 16)77 for i in range(1, lim + 1):78 if s[:i] == s[i:2 * i] and 2 * i <= m:79 split_points.append(i)80 if s[m - i:] == s[m - 2 * i:m - i] and m - i > 0:81 split_points.append(m - i)8283 seen = set()84 for p in split_points:85 if p <= 0 or p >= m or p in seen:86 continue87 seen.add(p)88 left = build(s[:p])89 right = build(s[p:])90 cand = ("S", left, right)91 c = enc_cost(left) + enc_cost(right)92 if c < best_cost:93 best = cand94 best_cost = c9596 memo[s] = best97 return best9899 root = build(data)100101 def decompress(node):102 t = node[0]103 if t == "L":104 return node[1]105 if t == "C":106 sub = decompress(node[1])107 if sub is None:108 return None109 return sub * node[2]110 if t == "S":111 a = decompress(node[1])112 if a is None:113 return None114 b = decompress(node[2])115 if b is None:116 return None117 return a + b118 return None119120 restored = decompress(root)121 if restored != data:122 return 999.0123124 ratio = enc_cost(root) / n125 return float(ratio)126 except:127 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