CF BUDDY
← Problems·

1948D · Tandem Repeats?

1700 · brute force, strings, two pointers

Problem: You are given a string ss, consisting of lowercase Latin letters and/or question marks.

A tandem repeat is a string of an even length such that its first half is equal to its second half.

A string aa is a substring of a string bb if aa can be obtained from bb by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Your goal is to replace each question mark with some lowercase Latin letter in such a way that the length of the longest substring that is a tandem repeat is maximum possible.

Input Format: The first line contains a single integer tt (1t10001 \le t \le 1000) — the number of testcases.

The only line of each testcase contains a string ss (1s50001 \le |s| \le 5000), consisting only of lowercase Latin letters and/or question marks.

The total length of the strings over all testcases doesn't exceed 50005000.

Output Format: For each testcase, print a single integer — the maximum length of the longest substring that is a tandem repeat after you replace each question mark in the string with some lowercase Latin letter.

If it's impossible to make any tandem repeat substrings in the string, print 00.

Sample Cases

Case 1

Input

4
zaabaabz
?????
code?????s
codeforces

Output

6
4
10
0

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback