Manacher
Manacher——最长回文字串
//#include<Windows.h> #include<iostream> #include<cstring> using namespace std; const int maxn = 100010; char Ma[maxn<< 1]; int Mp[maxn<< 1]; void Manacher(char s[], int len) { int l = 0; Ma[l++] = '$'; Ma[l++] = '#'; for (int i = 0; i < len; i++) { Ma[l++] = s[i]; Ma[l++] = '#'; } Ma[l++] = 0; int mx = 0, id = 0; for (int i = 0; i < l; i++) { Mp[i] = mx > i ? min(Mp[2* id - i], mx - i) : 1; while (Ma[i + Mp[i]] == Ma[i - Mp[i]]) Mp[i]++; if (i + Mp[i] > mx) { mx = i + Mp[i]; id = i; } } } char ss[maxn]; int main() { cin >> ss; int len = strlen(ss); Manacher(ss, len); int ans = 0; for (int i = 0; i < 2* len+ 2; i++) { ans = max(ans, Mp[i] - 1); /*cout << Ma[i] << ": " << Mp[i] << endl;*/ } printf("%d\n", ans); // system("pause"); return 0; }
代码意图很明显了,注释什么的就不需要了。