Description
Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, “Can you propose an efficient algorithm to find the length of the largest palindrome in a string?”
A string is said to be a palindrome if it reads the same both forwards and backwards, for example “madam” is a palindrome while “acm” is not.
The students recognized that this is a classical problem but couldn’t come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said “Okay, I’ve a better algorithm” and before he starts to explain his idea he stopped for a moment and then said “Well, I’ve an even better algorithm!”.
If you think you know Andy’s final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string “END” (quotes for clarity).
Output
For each test case in the input print the test case number and the length of the largest palindrome.
Sample Input
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6
字符串哈希+二分
1.对于奇数回文串求一个最大的整数p
,使得S[i-p ~ i]=reverse(S[i ~ i+p])
,那么以i
为中心的最长奇数回文子串长度为2*p+1
2.对于偶数回文串求一个最大的整数q
,使得S[i-q ~ i-1]=reverse(S[i ~ i+q-1])
,那么以i-1
和i
之间的夹缝为中心的最长偶数回文子串长度为2*p
1 i 13
奇数:a b c b a b c b a b c b a
Hash(1,7)=ReverseHash(7,13)
1 7 i 12
偶数:a b a c a c b a a | a a b
Hash(7,9)=ReverseHash(10,12)
在O(n)
的时间复杂度下求出正向Hash
和反向Hash
,在通过二分p 、q
在O(logn)
的时间内求出回文长度的最大值,整体时间复杂度为O(nlogn)
,判断是否相同比较二者的哈希值即可
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
using ull = unsigned long long;
const int maxn = 1000005;
ull fhash[maxn], bhash[maxn], p[maxn];//正向哈希,反向哈希,保存131次方的值
char s[maxn]; //保存字符串
int ans = 1, cnt = 0, len;//最短回文长度1,自己和自己回文,不过poj没卡这种数据
bool checkOdd(int pos)//检查奇数回文子串
{
for (int i = pos + 1; i <= len - pos; ++i)
{
int left = fhash[i - 1] - fhash[i - pos - 1] * p[pos]; //左向右读
int right = bhash[len - i] - bhash[len - i - pos] * p[pos]; //右向左读
if (left == right)
{
ans = max(ans, 2 * pos + 1);
return true;
}
}
return false;
}
bool checkEven(int pos)//检查偶数回文子串
{
for (int i = pos + 1; i <= len - pos + 1; ++i)
{
int left = fhash[i - 1] - fhash[i - pos - 1] * p[pos]; //左向右读
int right = bhash[len - i + 1] - bhash[len - i + 1 - pos] * p[pos]; //右向左读
if (left == right)
{
ans = max(ans, 2 * pos);
return true;
}
}
return false;
}
int main()
{
while (scanf("%s", s + 1) && s[1] != 'E')
{
len = strlen(s + 1);
p[0] = 1;
int temp = len;
for (int i = 1; i <= len; ++i)
{
fhash[i] = fhash[i - 1] * 131 + (s[i] - 'a' + 1); //从前向后的哈希
bhash[i] = bhash[i - 1] * 131 + (s[temp--] - 'a' + 1); //从后向前的哈希
p[i] = p[i - 1] * 131;
}
int l = 0, r = len;
while (l < r)
{
int mid = l + r + 1 >> 1;
if (checkOdd(mid) || checkEven(mid)) //当前半径可以有,向大寻找
l = mid;
else
r = mid - 1;
}
printf("Case %d: %d\n", ++cnt, ans);
ans = 1; //更新,防止上一次影响
}
return 0;
}