Problem list
Log in
#usaco0629 · USACO 2024-2025 December Bronze

It's Mooin' Time

Gold IV 2.0 s 256 MB

Farmer John is trying to describe his favorite USACO contest to Elsie, but she is having trouble understanding why he likes it so much. He says "My favorite part of the contest was when Bessie said 'It's Mooin' Time' and mooed all over the contest."

Elsie still doesn't understand so Farmer John downloads the contest as a text file and tries to explain what he means. The contest is defined as a string of lowercase letters of length $N$ ($3 \leq N \leq 20,000$). A moo is generally defined as the substring $c_ic_jc_j$ where some character $c_i$ followed directly by $2$ occurrences of some character $c_j$ where $c_i \neq c_j$. According to Farmer John, Bessie moos a lot, so if some moo appears at least $F$ ($1\le F\le N$) times in the contest, that might be from Bessie.

However, Farmer John's download might have been corrupted, and the text file might have up to one character that differs from the original file. Print all possible moos that Bessie could have made taking the potential error into account, sorted in alphabetical order.

Input

The first line contains $N$ and $F$, representing the length of the string and the frequency threshold for a moo by Bessie.

The second line contains a string of lowercase letters of length $N$, representing the contest.

Output

Print out the number of possible moos that Bessie makes, followed by a lexicographically sorted list of the moos. Each moo should appear on a separate line.

Constraints

SCORING

  • Inputs 4-8: $N \le 100$
  • Inputs 9-13: No additional constraints.

Example 1

Input
10 2
zzmoozzmoo
Output
1
moo

In this case, no character change affects the answer. The only moo Bessie made was "moo".

Example 2

Input
17 2
momoobaaaaaqqqcqq
Output
3
aqq
baa
cqq

In this case, the $a$ at position $8$ (zero-indexed) could have been corrupted from a $b$ which would have resulted in "baa" being a moo that Bessie made twice. Alternatively, the $q$ at position $11$ could have been corrupted from a $c$ which would have resulted in "cqq" being a possible moo that Bessie made. "aqq" can be made by swapping the $c$ with an $a$.

Example 3

Input
3 1
ooo
Output
25
aoo
boo
coo
doo
eoo
foo
goo
hoo
ioo
joo
koo
loo
moo
noo
poo
qoo
roo
soo
too
uoo
voo
woo
xoo
yoo
zoo

Problem credits: Suhas Nagar

Source
USACO 2024-2025 December Bronze
Appears in
Not part of a ladder, contest or set yet.
Accepted 1 Submissions 1 Acceptance 100% Solvers 1

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code