可以在O(n)时间复杂度求回文串长度
1 void INIT(string str){ 2 int i, len = str.size(); 3 temp += '@'; 4 for(int i = 1; i <= 2*len; i+=2){ 5 temp += '#'; 6 temp += str[i/2]; 7 } 8 temp += '#'; 9 temp += '$'; 10 // temp += 0; 11 } 12 void MANACHER(string str){ 13 int mx = 0, ans = 0,po = 0; 14 for(int i = 1; i < str.size(); i++){ 15 if(mx > i) len[i] = min(mx-i, len[2*po-i]); 16 else len[i] = 1; 17 while(str[i-len[i]] == str[i+len[i]]) len[i]++; 18 if(len[i]+i > mx) { 19 mx = len[i]+i; 20 po = i; 21 } 22 } 23 }