class Solution {
public static char[] preProcess(String s){
int len = s.length();
char[] t = new char[len*2+3];
t[0] = '$';
t[1] = '#';
int j = 2;
for(int i = 0;i < len;i++){
t[j++] = s.charAt(i);
t[j++] = '#';
}
t[j] = '\0';
return t;
}
public static int Manacher(String s){
if(s==null)return 0;
int[] Len = new int[s.length()*2+3];
char[] t = preProcess(s);
int p=0, po=0,max_len = 0;
for(int i = 1;i < t.length-1;i++){
if(i < p){
Len[i] = Math.min(p-i, Len[2*po-i]);
}
else Len[i] = 1;
while(t[i-Len[i]] == t[i+Len[i]])Len[i]++;
if(i+Len[i]-1 > p){
po=i;
p = i+Len[i]-1;
}
max_len = Math.max(max_len, Len[i]);
}
return max_len;
}
public static void main(final String[] args) {
}
}
[模板]——马拉车算法[java]
最新推荐文章于 2021-07-29 10:19:26 发布