## Gatevin的专栏

Another blog address: gatevin.moe

# URAL 1297 Palindrome 后缀数组 或 Manacher 求最长回文子串

Result  :  Accepted     Memory  :  610 KB     Time  :  15 ms

/*
* Author: Gatevin
* Created Time:  2015/2/9 18:54:36
* File Name: Iris_Freyja.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 2333

/*
* Doubling Algorithm 求后缀数组
*/
int wa[maxn], wb[maxn], wv[maxn], Ws[maxn];

int cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a + l] == r[b + l];
}

void da(int *r, int *sa, int n, int m)
{
int *x = wa, *y = wb, *t, i, j, p;
for(i = 0; i < m; i++) Ws[i] = 0;
for(i = 0; i < n; i++) Ws[x[i] = r[i]]++;
for(i = 1; i < m; i++) Ws[i] += Ws[i - 1];
for(i = n - 1; i >= 0; i--) sa[--Ws[x[i]]] = i;
for(j = 1, p = 1; p < n; j *= 2, m = p)
{
for(p = 0, i = n - j; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(i = 0; i < n; i++) wv[i] = x[y[i]];
for(i = 0; i < m; i++) Ws[i] = 0;
for(i = 0; i < n; i++) Ws[wv[i]]++;
for(i = 1; i < m; i++) Ws[i] += Ws[i - 1];
for(i = n - 1; i >= 0; i--) sa[--Ws[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
}
return;
}

int rank[maxn], height[maxn];
void calheight(int *r, int *sa, int n)
{
int i, j, k = 0;
for(i = 1; i <= n; i++) rank[sa[i]] = i;
for(i = 0; i < n; height[rank[i++]] = k)
for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);
return;
}

//RMQ预处理
int dp[maxn][20];

void initRMQ(int N)
{
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]);
return;
}

int askRMQ(int a, int b)
{
int ra = rank[a], rb = rank[b];
if(ra > rb) swap(ra, rb);
int k = 0;
while((1 << (k + 1)) <= rb - ra) k++;
return min(dp[ra + 1][k], dp[rb - (1 << k) + 1][k]);
}

char in[maxn >> 1];
int s[maxn], sa[maxn];
int n;

int main()
{
scanf("%s", in);
n = strlen(in);
for(int i = 0; i < n; i++)
if(in[i] >= 'a' && in[i] <= 'z')
s[i] = in[i] - 'a' + 1;
else
s[i] = in[i] - 'A' + 27;
s[n] = 53;
for(int i = n - 1; i >= 0; i--)
if(in[i] >= 'a' && in[i] <= 'z')
s[2*n - i] = in[i] - 'a' + 1;
else
s[2*n - i] = in[i] - 'A' + 27;
s[2*n + 1] = 0;
da(s, sa, 2*n + 2, 54);
calheight(s, sa, 2*n + 1);
initRMQ(2*n + 1);
int start = 0;
int anslen = 0;
/*
* 枚举回文串的中间位置为字符i
* 如果回文串长度为奇数则i是正中间的字符位置,对应的镜面出来的位置是2*n - i
* 查询LCP(Suffix(i), Suffix(2*n - i))的值为L则回文串长为2*L - 1
* 如果回文串长为偶数,i代表的是对称轴左边第一个字符, 对应镜面出来的位置是2*n - i
* 查询LCP(Suffix(i + 1), Suffix(2*n - i))的值为L则回文串长为2*L
* 注意判断长度出界的情况即可
* 时间复杂度O(nlogn)
* 可以优化RMQ至O(n)处理, 使用DC3算法求后缀数组, 这样整个问题可以降至O(n)复杂度
*/
for(int i = 0; i < n; i++)
{
int L = askRMQ(i, 2*n - i);//如果回文串长度为奇数
if(2*L - 1 > anslen && i >= L - 1)
{
anslen = 2*L - 1;
start = i - (L - 1);
}
if(i < n - 1)//如果是偶数
{
int L = askRMQ(i + 1, 2*n - i);
if(2*L > anslen && i >= L - 1)
{
anslen = 2*L;
start = i - (L - 1);
}
}
}
for(int i = 0; i < anslen; i++)
printf("%c", in[i + start]);
return 0;
}

Result  :  Accepted     Memory  :  374 KB     Time  :  15 ms

/*
* Author: Gatevin
* Created Time:  2015/3/20 10:44:13
* File Name: Chitoge_Kirisaki.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn 2010

void manacher(char *s, int *R, int n)
{
int p = 0, mx = 0;
R[0] = 1;
for(int i = 1; i < n; i++)
{
if(mx > i)
R[i] = min(R[2*p - i], mx - i);
else R[i] = 1;
while(s[i + R[i]] == s[i - R[i]])
R[i]++;
if(i + R[i] > mx)
p = i, mx = i + R[i];
}
return;
}

char in[maxn], s[maxn];
int R[maxn];

int main()
{
scanf("%s", in);
s[0] = '@';
int len = strlen(in);
for(int i = 0; i < len; i++)
s[2*i + 1] = in[i], s[2*i + 2] = '#';
s[2*len + 1] = '\$';
s[2*len + 2] = '\0';
int n = strlen(s);
manacher(s, R, n);
int maxlen = 0, pos = 0;
for(int i = 1; i < n; i++)
if(i & 1)
{
int len = (((R[i] - 1) >> 1) << 1) + 1;
if(len > maxlen)
maxlen = len, pos = i;
}
else
{
int len = (R[i] >> 1) << 1;
if(len > maxlen)
maxlen = len, pos = i;
}
for(int i = pos - (maxlen - 1); i <= pos + (maxlen - 1); i += 2)
printf("%c", s[i]);
return 0;
}

#### ural1297之最长回文子串

2013-11-29 16:39:27

#### 51nod 1089 最长回文子串 V2 （Manacher算法）

2015-10-30 10:11:40

#### 求最长回文子串的Manacher算法

2011-07-24 22:25:05

#### Manacher算法实现求最长回文子串的长度

2016-09-02 14:52:31

#### 【Ural 1297】Palindrome manacher 最长回文子串

2017-03-17 19:16:04

#### URAL 1297 Palindrome(最长回文子串:后缀数组)

2014-04-17 17:11:18

#### URAL - 1297 Palindrome(后缀数组求最长回文子串)

2014-09-26 21:05:45

#### 【URAL】1297 Palindrome 【后缀数组+RMQ——求最长回文子串】

2014-12-11 22:19:44

#### URAL 1297 Palindrome【后缀数组】求最长回文子串

2016-10-11 03:22:27