A repeated string is composed of two identical strings. For example, abcabc is a repeated string with length of 6, while abcba does not have a duplicate string. Given any string, please help Xiaoqiang find the longest repeated substring.

a repeating string is a concatenation of two identical strings. For example, abcabc is a repeating string of length 6, while abcba does not have a repeating string.
given any string, find the longest repeating substring.

input description :

enter a string s, where s is less than 1e4 and contains only Numbers and letters.

output description :

outputs an integer representing the length of the longest repeating substring of s. If it does not exist, 0

is output

input example 1:

xabcabcx

output example 1:

6

def getMaxRepeatSubstringLength(inputStr):
    length = len(inputStr)
    for i in reversed(range(length//2)):
        count = 0
        for j in range(length - i):
            if inputStr[j] == inputStr[j+i]:
                count = count + 1
            else:
                if length - j <= 2 * i:
                    break
                count = 0
            if count == i:
                return count * 2
    return 0
inputStr = input()
print(getMaxRepeatSubstringLength(inputStr))

1. First assume the length of the longest repeating substring is I
2. Judge whether the length of the assumption is I is true
3. The longest repeating substring cannot be more than half of the total length
4. In the process of determining whether the hypothesis is true, if the remaining length to be determined is less than 2* I, the hypothesis must be false
5. Since it is the judgment of the eldest string, it is reasonable to assume that I is from large to small, and once it is met, you can withdraw directly. Whereas the assumption from small to large is to check all the cases.

Read More: