Secret Message
Time limit1sMemory limit128 MB
Count the operation sequences that build the given string by repeatedly prepending or appending a proper prefix or suffix.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String, String matching
- Solved
- No attempts yet
Problem
Sanggeun received a secret message from Changyeong. The message uses only uppercase letters, and its length is at least 2.
Decoding the message takes the following operation. Delete a few characters from the front of the string , or delete a few characters from the back of , and take what remains as a piece. Then glue that piece onto the front or the back of . The number of deleted characters is at least 1 and smaller than the length of , so the piece is never empty and never equal to all of .
For example, there are 8 ways to apply the operation to the string ABC.
- AABC (A + ABC)
- ABABC (AB + ABC)
- BCABC (BC + ABC)
- CABC (C + ABC)
- ABCA (ABC + A)
- ABCAB (ABC + AB)
- ABCBC (ABC + BC)
- ABCC (ABC + C)
You are given the string Sanggeun finished decoding. Write a program that counts how many ways there are to build it. The operation may be applied several times. Two ways count as different when the sequence of operations differs, even if they produce the same string. For example, there are 4 ways to build AAA from AA.
Input
The first line contains the decoded string. It consists of uppercase letters only, and its length is at least 2 and at most 100.
Output
Print the number of ways to build the given string. The starting message must have length at least 2. The count can grow very large, so print it modulo 2014. If the string cannot be built at all, print 0.
Hint
There are 8 ways to build ABABA.
- start from ABA -> AB + ABA
- start from ABA -> ABA + BA
- start from AB -> AB + A -> AB + ABA
- start from AB -> AB + A -> ABA + BA
- start from BA -> A + BA -> AB + ABA
- start from BA -> A + BA -> ABA + BA
- start from ABAB -> ABAB + A
- start from BABA -> A + BABA