Cheap AI

Time limit: 2s Memory limit: 64MB Input: Output:

You are a small, stressed mouse trying to survive in the ever–accelerating rise of AI startups. With competitors popping up everywhere, gaining any technological edge feels nearly impossible. In your desperate search for an advantage, you stumble upon CheapAITM\text{CheapAI}^{\text{TM}}, a startup so underfunded that even you can afford to work there. Your very first assignment: construct the world's cheapest tokenizer.

You have been hired by CheapAI and assigned the task of building a tokenizer. Unfortunately, the budget is very small, and besides the 26 letters of the English alphabet, you can afford only one additional token, of length at most KK.

You are given a string SS consisting of lowercase English letters and a number KK. Your goal is to choose a token string of length at most KK such that, if you replace some non-overlapping occurrences of your choice of this token in the string with the special character #, the total number of characters required to represent the resulting string is minimized.

Task

Given a number KK and a string SS consisting of lowercase English letters, choose a non-empty string tt, with 1tK1 \le |t| \le K, such that by replacing each occurrence of tt in SS (chosen so that no two overlap) with the special character #, the total length of the resulting string is minimized.

Determine this minimum length.

Implementation

You should implement the following function:

int solve(int K, std::string S);

This function receives KK and SS as parameters and must determine the minimum length of the string obtained after replacing some (non-overlapping) occurrences of a chosen token, of length at most KK, with the character #.

Constraints

  • 1KS200 0001 \leq K \leq |S| \leq 200 \ 000
  • SS consists of lowercase English letters.
# Points Constraints
1 5 Si=aS_i = a, for all 1iS1 \leq i \leq \lvert S \rvert
2 7 S100\lvert S \rvert \leq 100
3 12 S5 000\lvert S \rvert \leq 5 \ 000
4 40 S75 000\lvert S \rvert \leq 75 \ 000
5 36 No additional constraints.

Example 1

input

5
aabaabacbbaabaa

output

7

Explanation

We choose tt = aabaa, and the string SS becomes #bacbb#, with length 77.

Example 2

input

8
aaaaaaaaaaaaaaaaaaa

output

4

Explanation

We choose tt = aaaaaa, and the string SS becomes ###a, of length 44.

Log in or sign up to be able to send submissions!