Chefstr2.py — различия между версиями
Материал из DISCOPAL
StasFomin (обсуждение | вклад) |
StasFomin (обсуждение | вклад) |
||
Строка 24: | Строка 24: | ||
for i in range(ind): | for i in range(ind): | ||
frequency = [0] * lenalf | frequency = [0] * lenalf | ||
+ | max_frequency = 0 | ||
j = i | j = i | ||
while j < n: | while j < n: | ||
− | frequency[input_idx[j]] + | + | cur_freq = frequency[input_idx[j]] + 1 |
+ | if cur_freq > max_frequency: | ||
+ | max_frequency = cur_freq | ||
+ | frequency[input_idx[j]] = cur_freq | ||
j += ind | j += ind | ||
− | cur_diff += ks - | + | cur_diff += ks - max_frequency |
if cur_diff < operations: | if cur_diff < operations: |
Версия 22:02, 4 мая 2022
import sys def main(): symbols = {} alphabet = "abcdefghijklmnopqrstuvwxyz" lenalf = len(alphabet) for i in range(lenalf): symbols[alphabet[i]] = i input_str = sys.stdin.readline()[:-1] n = len(input_str) input_idx = bytearray([symbols[ch] for ch in input_str]) operations = 1e6 k_pow = 1e6 ind = 1 lim = 3 * n / 4 while ind <= lim: cur_diff = 0 ks = -(n // -ind) for i in range(ind): frequency = [0] * lenalf max_frequency = 0 j = i while j < n: cur_freq = frequency[input_idx[j]] + 1 if cur_freq > max_frequency: max_frequency = cur_freq frequency[input_idx[j]] = cur_freq j += ind cur_diff += ks - max_frequency if cur_diff < operations: operations = cur_diff k_pow = ks elif operations == cur_diff: if ks < k_pow: k_pow = ks ind += 1 print(f"{operations} {k_pow}") main()