做法:
- 最长回文子串两种解法:
- 1. Manacher 复杂度O(n)
- 2. 后缀数组+RMQ 复杂度O(n*log(n))
AC代码:
//Manacher
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x) push_back(x)
#define sz(x) (int)(x).size()
#define sc(x) scanf("%d",&x)
#define abs(x) ((x)<0 ? -(x) : x)
#define all(x) x.begin(),x.end()
#define mk(x,y) make_pair(x,y)
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e6;
const int maxn = 1e6+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<62;
char s[maxn];
char ma[maxn<<1]; //处理好后的字符串
int mp[maxn<<1]; //代表以字符串第i位为中心的回文串的最大长度
int res;
int 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'; //id代表当前“已经匹配完毕的结尾最远的回文串”中心为Ma[]数组的第ID为
int id = 0,mx = 0;//mx代表当前“已经匹配完毕的结尾最远的回文串”到达了Ma[]数组的第Mx位
int ans = -1;
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;
}
if(mp[i]-1>ans)
{
res = i;
ans = mp[i]-1;
}
// ans = max(ans,mp[i]-1);
}
return ans;
}
int main()
{
// fin;
IO;
cin>>s;
int n = strlen(s);
int len = manacher(s,n)*2;
int ansl = res-len/2;
int ansr = res+len/2;
for(int i=ansl;i<=ansr;i++)
{
if(ma[i]!='$' && ma[i]!='#') cout<<ma[i];
}
return 0;
}
//后缀数组+RMQ
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x) push_back(x)
#define sz(x) (int)(x).size()
#define sc(x) scanf("%d",&x)
#define abs(x) ((x)<0 ? -(x) : x)
#define all(x) x.begin(),x.end()
#define mk(x,y) make_pair(x,y)
#define debug printf("!!!!!!\n")
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e6;
const int maxn =1e4+10;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<62;
const int m = 128; //
string s0,s1;
int dp[maxn][30];
char s[maxn]; //待排序的字符串放在s数组中,从s[0~n-1],长度为n,且最大值小于m,最后一位是0(无效值)
int sa[maxn],t1[maxn],t2[maxn],c[maxn],n; //sa[1~n]为有效值,sa[0]必定为n是无效值
int rk[maxn]; //rank[0~n-1]为有效值,rank[n]必定为0无效值
int height[maxn]; //height[1~n]
//lcp(x,y):字符串x与字符串y的最长公共前缀,在这里指x号后缀与与y号后缀的最长公共前缀
//height[i]:lcp(sa[i],sa[i-1]),即排名为i的后缀与排名为i-1的后缀的最长公共前缀
//H[i]:height[rak[i]],即i号后缀与它前一名的后缀的最长公共前缀
void build_sa(int m)
{
n++;
int *x = t1,*y = t2;
for(int i=0;i<m;i++) c[i] = 0;
for(int i=0;i<n;i++) c[x[i] = s[i]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[i]]] = i;
for(int j=1;j<=n;j<<=1)
{
int p = 0;
for(int i = n-j;i<n;i++) y[p++] = i;
for(int i=0;i<n;i++) if(sa[i]>=j) y[p++] = sa[i]-j;
for(int i=0;i<m;i++) c[i] = 0;
for(int i=0;i<n;i++) c[x[y[i]]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = 1;x[sa[0]] = 0;
for(int i=1;i<n;i++)
x[sa[i]] = (y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+j] == y[sa[i]+j])?p-1:p++;
if(p>=n) break;
m = p;
}
n--;
int k = 0;
for(int i=0;i<=n;i++) rk[sa[i]] = i;
for(int i=0;i<n;i++){
if(k) k--;
int j = sa[rk[i]-1];
while(s[i+k] == s[j+k]) k++;
height[rk[i]] = k;
}
}
void init_rmq()
{
for(int i=1;i<=n;i++) dp[i][0] = height[i];
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)
dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
int query(int i,int j)
{
int l = rk[i];
int r = rk[j];
if(l>r) swap(l,r);
++l;
int k = 0;
while(1<<(k+1)<=r-l+1) k++;
return min(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main()
{
// fin;
IO;
cin>>s0;
s1 = s0;
int nn = sz(s0);
reverse(all(s0));
s1 += "#"+s0;
strcpy(s,s1.c_str());
n = strlen(s); //n为将原字符串翻转用分隔符接在原字符串后的长度
build_sa(m);
init_rmq();
int ans = -1,pos = 0;
for(int i=0;i<nn;i++) //nn为原字符串长度
{
if(2*query(i+1,n-i)+1>ans){ //长度为奇数,求以i为中心,i+1和(i-1反转后)后缀的LCA
ans = 2*query(i+1,n-i)+1;
pos = i-query(i+1,n-i);
}
if(2*query(i,n-i)>ans){ //长度为偶数,求以i为中心,i和(i-1反转后)后缀的LCA
ans = 2*query(i,n-i);
pos = i-query(i,n-i);
}
}
for(int i = pos;i<pos+ans;i++) cout<<s[i];
return 0;
}