代码:
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
const double N = 1e6+10;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const inline int read(){
int k = 0, f = 1; char c = getchar();
for(;!isdigit(c); c = getchar())
if(c == '-') f = -1;
for(;isdigit(c); c = getchar())
k = k * 10 + c - '0';
return k * f;
}
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 200050
char Ma[MAXN*2];
int Mp[MAXN*2];
int ans = 0;
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-1],mx-1):1;
if(mx>i)
Mp[i]=min(mx-i,Mp[2*id-i]);//核心
else
Mp[i]=1;
while(Ma[i+Mp[i]]==Ma[i-Mp[i]])
Mp[i]++;
if(i+Mp[i] > mx)
{
mx = i + Mp[i];
id = i;
}
ans = max(ans,Mp[i]);
}
}
char s[MAXN];
int main()
{
cin >> s;
int len = strlen(s);
Manacher(s,len);
// for(int i = 0 ; i < 2*len+1 ; i++)
// {
// ans = max(ans,Mp[i]-1);
// }
cout << ans-1 << endl;
return 0;
}
这道理让我学到了,三目运算符会超时,改了一中午,改成ifelse竟然A了,裂开。