templete_最长回文子串manachar

来源:互联网 发布:淘宝买瞄准镜违法吗 编辑:程序博客网 时间:2024/06/07 01:18
#include <stdio.h>#include <string.h>#include <iostream>#include <string>using namespace std;const int MAXN = 110000 + 11;char b[MAXN];char str[MAXN * 2];int p[MAXN * 2];int manachar(){int temp;memset(p, 0, sizeof(p));for (int i = 1; b[i] != '\0'; i++){str[i << 1] = b[i];str[i << 1 | 1] = '#';temp = i;}str[1] = '#';str[0] = '$';int n = temp * 2 + 2;str[n] = '\0';int id = 0;int MaxId = 0;int MaxL = 0;for (int i = 1; i < n; i++){if (i < MaxId){p[i] = min(p[2 * id - i], MaxId - i);}else{p[i] = 1;}while (str[p[i] + i] == str[i - p[i]]){p[i]++;}if (p[i] + i > MaxId){MaxId = p[i] + i;id = i;}if (p[i] > MaxL){MaxL = p[i];}}return MaxL - 1;}int main(){while (scanf("%s", b + 1) != EOF){int ans = manachar();printf("%d\n", ans);}//system("pause");return 0;}

原创粉丝点击