TopcoderArchive
Archive22 Aug 2006
TCCC06 Round 2A

Repeat

Problem statement, definition, constraints, and public examples.

Problem Statement

We have come up with a compression technique for lowercase text that contains a lot of repetition. We will use 'R' to mean repeat the preceding text. The area to be repeated will be all the text since the closest preceding 'M' (or since the beginning of the text if there is no preceding 'M').

For example, "abcabcdabcabcdxyxyz" could be expressed in compressed form as "abcRdRMxyRz". To decompress this, the beginning abc is first repeated to become "abcabc", then "d" is added, and then all of that is repeated to make "abcabcdabcabcd". This is followed by the "xy" which is repeated and followed by the "z".

Create a class Repeat that contains a method minLength that is given the text to be compressed and that returns the minimum number of characters that can be used to express it with this compression technique. Of course all the 'R' and 'M' characters must be counted.

Definition

Class:
Repeat
Method:
minLength
Parameters:
String
Returns:
int
Method signature:
int minLength(String text)
(be sure your method is public)

Constraints

  • text will contain between 1 and 50 characters, inclusive.
  • Each character in text will be a lowercase letter 'a'-'z'.

Examples

  1. "aaaaaaaa"
    Returns: 4
    Either "aRRR" or "aaRR" is a way of compressing this to 4 characters.
  2. "aaaaaaa"
    Returns: 5
    One way is with "aaaRa"
  3. "bcdcdcdcdxcdcdcdcd"
    Returns: 12
    One way is "bMcdRRxMcdRR"
  4. "xyz"
    Returns: 3
    You can always use no 'R's or 'M's.
Back to all problems