Repetitive String Invention
InterviewTime limit2sMemory limit2048 MB
Count the ways to pick two non-overlapping substrings, in order, whose concatenation is a repetitive string (a square) for a string of length up to 800.
- Level
Medium5 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Problem
Lulu has a string of lowercase English letters. She wants to make it Repetitive. A repetitive string has an even number of characters, and its first half exactly matches its second half. For example, "lulu", "abcabc", and "xx" are repetitive, while "xyx" and "abac" are not.
To get a repetitive string, Lulu takes two non-overlapping, non-empty substrings of her string and concatenates them in the order they appear in the string.
In how many ways can she choose two substrings to form a repetitive string? Two ways are different if at least one of the substrings comes from a different part of the string.
Consider the string "aaaa".
- There are six ways to form "aa" by matching each "a" with a later "a" (+, +, +, +, +, +).
- There are three ways to form "aaaa": "a"+"aaa", "aa"+"aa", and "aaa"+"a".
So there are nine ways to form a repetitive string from "aaaa" this way.
Input
The only line contains a string of lowercase English letters, with .
Output
Print a single integer: the number of ways Lulu can choose two non-overlapping, non-empty substrings and concatenate them in order to get a repetitive string.