Nested Repetition Compression

시간 제한2초메모리 제한1024 MB

요약
소문자 문자열을 한 자리 반복 횟수와 중첩 괄호를 사용해 가장 짧게 압축한 표현을 찾는다.
난이도

보통10점 중 7점

유형
동적 계획법, 문자열, 구현
정답자
아직 제출이 없습니다

문제

You have a number of strings of lowercase letters to be sent in e-mails, but some of them are so long that typing as they are would be tiresome. As you found repeated parts in them, you have decided to try out a simple compression method in which repeated sequences are enclosed in parentheses, prefixed with digits meaning the numbers of repetitions. For example, the string “abababaaaaa” can be represented as “3(ab)5(a)” or “a3(ba)4(a)”. The syntax of compressed representations is given below in Backus-Naur form with the start symbol SS.

<SS> ::= <RR> | <RR> <SS>

<RR> ::= <LL> | <DD> (<SS>)

<DD> ::= 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

<LL> ::= a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z

Note that numbers of repetitions are specified by a single digit, and thus at most nine, but more repetitions can be specified by nesting them. A string of thirty a’s can be represented as “6(5(a))” or “3(5(2(a)))”, for example.

Find the shortest possible representation of the given string in this compression scheme.

입력

The input is a line containing a string of lowercase letters. The number of letters in the string is at least one and at most 200200.

출력

Output a single line containing the shortest possible representation of the input string. If there exist two or more shortest representations, any of them is acceptable.

예제3

  1. 예제 1

    입력
    abababaaaaa
    
    예상 출력
    3(ab)5(a)
    
  2. 예제 2

    입력
    abababcaaaaaabababcaaaaa
    
    예상 출력
    2(3(ab)c5(a))
    
  3. 예제 3

    입력
    abcdefg
    
    예상 출력
    abcdefg