import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param a string字符串 待计算字符串
* @return int整型
*/
public int solve (String str) {
int max = 0;
for (int p = 1; p < str.length(); p++) {
int n = Math.min(p, str.length() - p);
do {
String frontStr = str.substring(p - n, p);
String backStr = str.substring(p, p + n);
if (frontStr.equals(backStr)) {
max = Math.max(max, n);
break;
}
n--;
}while (n > max);
}
return max * 2;
}
}
搞个浮标p在字符串里移动,还有一个计数器n代表当前子串的长度,浮标每向后移动一位,初始化计数器n为浮标前方或子串的长度取最小,取浮标前长度为n的子串和浮标后长度为n的子串,比是否相同,若不同则n - 1,若相同记录最大值,n - 1 减到最大值就停止,浮标继续向后移动



京公网安备 11010502036488号