
1. A double string is a string that is made by concatenation of some string with itself.
2. The length of a double string is at least 2.
In the string ‘ababab’, there are just two substrings, that are double strings. Both such substrings are ‘abab’, and thus there exists only one distinct substring which is a double string. ‘abab’ can be written as ‘ab’ + ‘ab’. But ‘ababab’ can not be considered as a double string because ‘ab’ has been concatenated twice with itself.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains the string ‘STR’.
For each test case, print a single integer, denoting the total number of distinct substrings which are also double strings.
Output for each test case will be printed in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= |STR| < 2000
Where ‘T’ denotes the number of test cases and |STR| represents the length of the string ‘STR’
Time Limit: 1sec
The approach of this problem is to find out all substrings of even length because for any string to be a double string, it must be of even length. For checking whether a substring is a double string or not, simply check whether all the characters in the first half of the string are equal to the corresponding characters in the second half or not.
The idea is to use Rolling Hash for this solution. We will select substrings from the given string and find out the hash value using rolling hash algorithm. If hash value of two substrings are equal, it means the strings are equal and we can make the desired string using the two substrings.
The steps for this are as follows: