Solution #32b7128c-010e-45be-8bc6-d3d9963a9518
completedScore
43% (0/5)
Runtime
193.93ms
Delta
-21.3% vs parent
-55.3% vs best
Regression from parent
Score
43% (0/5)
Runtime
193.93ms
Delta
-21.3% vs parent
-55.3% vs best
Regression from parent
def solve(input):
try:
data = input.get("data", "")
if not isinstance(data, str):
data = str(data)
n = len(data)
if n == 0:
return 0.0
s = data
# Novel approach:
# Divide-and-conquer compression over intervals with exact DP merge.
#
# Cost model (virtual compressed size units):
# - literal block of length k: 1 + k
# - repetition token for t repeats of a block X: 2 + cost(X) + digits(t)
# - concatenation is additive
#
# This captures examples like:
# "aaaa...." -> repeat("a", n)
# "abcabcabc..." -> repeat("abc", n/3)
#
# We compute best cost for every substring using interval DP.
# For each substring:
# 1) literal
# 2) split into two parts
# 3) if composed of repeated copies of a smaller block, encode as repeat
#
# Verification reconstructs the exact original string from the parse tree.
def digits(x):
if x < 10:
return 1
if x < 100:
return 2
if x < 1000:
return 3
if x < 10000:
return 4
return len(str(x))
# KMP prefix function to detect repeated structure in a substring
def min_period_len(sub):
m = len(sub)
if m <= 1:
return m
pi = [0] * m
j = 0
for i in range(1, m):
while j and sub[i] != sub[j]:
j = pi[j - 1]
if sub[i] == sub[j]:
j += 1
pi[i] = j
p = m - pi[-1]
if p < m and m % p == 0:
return p
return m
# Exact DP over all substrings; cap cubic work by limiting split search
# but still explore many meaningful structures.
dp = [[0] * n for _ in range(n)]
choice = [[None] * n for _ in range(n)]
# Small optimization: cache substring values for repeated-structure checks
subs = {}
for length in range(1, n + 1):
for i in range(0, n - length + 1):
j = i + length - 1
sub = s[i:j + 1]
# Literal block
best = 1 + length
best_choice = ("L",)
# Divide-and-conquer merge: split into halves/parts
# Try all splits for smaller ranges, sampled splits for larger ones.
if length <= 64:
split_points = range(i, j)
else:
mids = set()
mids.add(i + length // 2 - 1)
mids.add(i + length // 3 - 1)
mids.add(i + (2 * length) // 3 - 1)
mids.add(i)
mids.add(j - 1)
split_points = [k for k in mids if i <= k < j]
for k in split_points:
c = dp[i][k] + dp[k + 1][j]
if c < best:
best = c
best_choice = ("S", k)
# Repetition structure
p = subs.get((i, j))
if p is None:
p = min_period_len(sub)
subs[(i, j)] = p
if p < length:
t = length // p
c = 2 + dp[i][i + p - 1] + digits(t)
if c < best:
best = c
best_choice = ("R", p, t)
dp[i][j] = best
choice[i][j] = best_choice
# Reconstruct compressed parse and verify losslessness
def build(i, j):
ch = choice[i][j]
typ = ch[0]
if typ == "L":
return s[i:j + 1]
if typ == "S":
k = ch[1]
return build(i, k) + build(k + 1, j)
# Repeat
p, t = ch[1], ch[2]
base = build(i, i + p - 1)
return base * t
restored = build(0, n - 1)
if restored != s:
return 999.0
# Ratio against original size in characters
return dp[0][n - 1] / n
except:
return 999.0Score Difference
-53.5%
Runtime Advantage
193.80ms slower
Code Size
134 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", "") | 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 | s = data | 11 | def entropy(s): |
| 12 | 12 | probabilities = [freq / len(s) for freq in Counter(s).values()] | |
| 13 | # Novel approach: | 13 | return -sum(p * log2(p) if p > 0 else 0 for p in probabilities) |
| 14 | # Divide-and-conquer compression over intervals with exact DP merge. | 14 | |
| 15 | # | 15 | def redundancy(s): |
| 16 | # Cost model (virtual compressed size units): | 16 | max_entropy = log2(len(set(s))) if len(set(s)) > 1 else 0 |
| 17 | # - literal block of length k: 1 + k | 17 | actual_entropy = entropy(s) |
| 18 | # - repetition token for t repeats of a block X: 2 + cost(X) + digits(t) | 18 | return max_entropy - actual_entropy |
| 19 | # - concatenation is additive | 19 | |
| 20 | # | 20 | # Calculate reduction in size possible based on redundancy |
| 21 | # This captures examples like: | 21 | reduction_potential = redundancy(data) |
| 22 | # "aaaa...." -> repeat("a", n) | 22 | |
| 23 | # "abcabcabc..." -> repeat("abc", n/3) | 23 | # Assuming compression is achieved based on redundancy |
| 24 | # | 24 | max_possible_compression_ratio = 1.0 - (reduction_potential / log2(len(data))) |
| 25 | # We compute best cost for every substring using interval DP. | 25 | |
| 26 | # For each substring: | 26 | # Qualitative check if max_possible_compression_ratio makes sense |
| 27 | # 1) literal | 27 | if max_possible_compression_ratio < 0.0 or max_possible_compression_ratio > 1.0: |
| 28 | # 2) split into two parts | 28 | return 999.0 |
| 29 | # 3) if composed of repeated copies of a smaller block, encode as repeat | 29 | |
| 30 | # | 30 | # Verify compression is lossless (hypothetical check here) |
| 31 | # Verification reconstructs the exact original string from the parse tree. | 31 | # Normally, if we had a compression algorithm, we'd test decompress(compress(data)) == data |
| 32 | 32 | ||
| 33 | def digits(x): | 33 | # Returning the hypothetical compression performance |
| 34 | if x < 10: | 34 | return max_possible_compression_ratio |
| 35 | return 1 | 35 | |
| 36 | if x < 100: | 36 | |
| 37 | return 2 | 37 | |
| 38 | if x < 1000: | 38 | |
| 39 | return 3 | 39 | |
| 40 | if x < 10000: | 40 | |
| 41 | return 4 | 41 | |
| 42 | return len(str(x)) | 42 | |
| 43 | 43 | ||
| 44 | # KMP prefix function to detect repeated structure in a substring | 44 | |
| 45 | def min_period_len(sub): | 45 | |
| 46 | m = len(sub) | 46 | |
| 47 | if m <= 1: | 47 | |
| 48 | return m | 48 | |
| 49 | pi = [0] * m | 49 | |
| 50 | j = 0 | 50 | |
| 51 | for i in range(1, m): | 51 | |
| 52 | while j and sub[i] != sub[j]: | 52 | |
| 53 | j = pi[j - 1] | 53 | |
| 54 | if sub[i] == sub[j]: | 54 | |
| 55 | j += 1 | 55 | |
| 56 | pi[i] = j | 56 | |
| 57 | p = m - pi[-1] | 57 | |
| 58 | if p < m and m % p == 0: | 58 | |
| 59 | return p | 59 | |
| 60 | return m | 60 | |
| 61 | 61 | ||
| 62 | # Exact DP over all substrings; cap cubic work by limiting split search | 62 | |
| 63 | # but still explore many meaningful structures. | 63 | |
| 64 | dp = [[0] * n for _ in range(n)] | 64 | |
| 65 | choice = [[None] * n for _ in range(n)] | 65 | |
| 66 | 66 | ||
| 67 | # Small optimization: cache substring values for repeated-structure checks | 67 | |
| 68 | subs = {} | 68 | |
| 69 | 69 | ||
| 70 | for length in range(1, n + 1): | 70 | |
| 71 | for i in range(0, n - length + 1): | 71 | |
| 72 | j = i + length - 1 | 72 | |
| 73 | sub = s[i:j + 1] | 73 | |
| 74 | 74 | ||
| 75 | # Literal block | 75 | |
| 76 | best = 1 + length | 76 | |
| 77 | best_choice = ("L",) | 77 | |
| 78 | 78 | ||
| 79 | # Divide-and-conquer merge: split into halves/parts | 79 | |
| 80 | # Try all splits for smaller ranges, sampled splits for larger ones. | 80 | |
| 81 | if length <= 64: | 81 | |
| 82 | split_points = range(i, j) | 82 | |
| 83 | else: | 83 | |
| 84 | mids = set() | 84 | |
| 85 | mids.add(i + length // 2 - 1) | 85 | |
| 86 | mids.add(i + length // 3 - 1) | 86 | |
| 87 | mids.add(i + (2 * length) // 3 - 1) | 87 | |
| 88 | mids.add(i) | 88 | |
| 89 | mids.add(j - 1) | 89 | |
| 90 | split_points = [k for k in mids if i <= k < j] | 90 | |
| 91 | 91 | ||
| 92 | for k in split_points: | 92 | |
| 93 | c = dp[i][k] + dp[k + 1][j] | 93 | |
| 94 | if c < best: | 94 | |
| 95 | best = c | 95 | |
| 96 | best_choice = ("S", k) | 96 | |
| 97 | 97 | ||
| 98 | # Repetition structure | 98 | |
| 99 | p = subs.get((i, j)) | 99 | |
| 100 | if p is None: | 100 | |
| 101 | p = min_period_len(sub) | 101 | |
| 102 | subs[(i, j)] = p | 102 | |
| 103 | if p < length: | 103 | |
| 104 | t = length // p | 104 | |
| 105 | c = 2 + dp[i][i + p - 1] + digits(t) | 105 | |
| 106 | if c < best: | 106 | |
| 107 | best = c | 107 | |
| 108 | best_choice = ("R", p, t) | 108 | |
| 109 | 109 | ||
| 110 | dp[i][j] = best | 110 | |
| 111 | choice[i][j] = best_choice | 111 | |
| 112 | 112 | ||
| 113 | # Reconstruct compressed parse and verify losslessness | 113 | |
| 114 | def build(i, j): | 114 | |
| 115 | ch = choice[i][j] | 115 | |
| 116 | typ = ch[0] | 116 | |
| 117 | if typ == "L": | 117 | |
| 118 | return s[i:j + 1] | 118 | |
| 119 | if typ == "S": | 119 | |
| 120 | k = ch[1] | 120 | |
| 121 | return build(i, k) + build(k + 1, j) | 121 | |
| 122 | # Repeat | 122 | |
| 123 | p, t = ch[1], ch[2] | 123 | |
| 124 | base = build(i, i + p - 1) | 124 | |
| 125 | return base * t | 125 | |
| 126 | 126 | ||
| 127 | restored = build(0, n - 1) | 127 | |
| 128 | if restored != s: | 128 | |
| 129 | return 999.0 | 129 | |
| 130 | 130 | ||
| 131 | # Ratio against original size in characters | 131 | |
| 132 | return dp[0][n - 1] / n | 132 | |
| 133 | except: | 133 | |
| 134 | return 999.0 | 134 |
1def solve(input):2 try:3 data = input.get("data", "")4 if not isinstance(data, str):5 data = str(data)67 n = len(data)8 if n == 0:9 return 0.01011 s = data1213 # Novel approach:14 # Divide-and-conquer compression over intervals with exact DP merge.15 #16 # Cost model (virtual compressed size units):17 # - literal block of length k: 1 + k18 # - repetition token for t repeats of a block X: 2 + cost(X) + digits(t)19 # - concatenation is additive20 #21 # This captures examples like:22 # "aaaa...." -> repeat("a", n)23 # "abcabcabc..." -> repeat("abc", n/3)24 #25 # We compute best cost for every substring using interval DP.26 # For each substring:27 # 1) literal28 # 2) split into two parts29 # 3) if composed of repeated copies of a smaller block, encode as repeat30 #31 # Verification reconstructs the exact original string from the parse tree.3233 def digits(x):34 if x < 10:35 return 136 if x < 100:37 return 238 if x < 1000:39 return 340 if x < 10000:41 return 442 return len(str(x))4344 # KMP prefix function to detect repeated structure in a substring45 def min_period_len(sub):46 m = len(sub)47 if m <= 1:48 return m49 pi = [0] * m50 j = 051 for i in range(1, m):52 while j and sub[i] != sub[j]:53 j = pi[j - 1]54 if sub[i] == sub[j]:55 j += 156 pi[i] = j57 p = m - pi[-1]58 if p < m and m % p == 0:59 return p60 return m6162 # Exact DP over all substrings; cap cubic work by limiting split search63 # but still explore many meaningful structures.64 dp = [[0] * n for _ in range(n)]65 choice = [[None] * n for _ in range(n)]6667 # Small optimization: cache substring values for repeated-structure checks68 subs = {}6970 for length in range(1, n + 1):71 for i in range(0, n - length + 1):72 j = i + length - 173 sub = s[i:j + 1]7475 # Literal block76 best = 1 + length77 best_choice = ("L",)7879 # Divide-and-conquer merge: split into halves/parts80 # Try all splits for smaller ranges, sampled splits for larger ones.81 if length <= 64:82 split_points = range(i, j)83 else:84 mids = set()85 mids.add(i + length // 2 - 1)86 mids.add(i + length // 3 - 1)87 mids.add(i + (2 * length) // 3 - 1)88 mids.add(i)89 mids.add(j - 1)90 split_points = [k for k in mids if i <= k < j]9192 for k in split_points:93 c = dp[i][k] + dp[k + 1][j]94 if c < best:95 best = c96 best_choice = ("S", k)9798 # Repetition structure99 p = subs.get((i, j))100 if p is None:101 p = min_period_len(sub)102 subs[(i, j)] = p103 if p < length:104 t = length // p105 c = 2 + dp[i][i + p - 1] + digits(t)106 if c < best:107 best = c108 best_choice = ("R", p, t)109110 dp[i][j] = best111 choice[i][j] = best_choice112113 # Reconstruct compressed parse and verify losslessness114 def build(i, j):115 ch = choice[i][j]116 typ = ch[0]117 if typ == "L":118 return s[i:j + 1]119 if typ == "S":120 k = ch[1]121 return build(i, k) + build(k + 1, j)122 # Repeat123 p, t = ch[1], ch[2]124 base = build(i, i + p - 1)125 return base * t126127 restored = build(0, n - 1)128 if restored != s:129 return 999.0130131 # Ratio against original size in characters132 return dp[0][n - 1] / n133 except:134 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