POJ 3974 - Palindrome - [字符串hash+二分]

题目链接:http://poj.org/problem?id=3974

Time Limit: 15000MS Memory Limit: 65536K

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

题意:

给出若干个字符串,求最长回文子串的长度。

题解:

首先预处理字符串的长度为 $i$ 的前缀子串的哈希值 $pre[i]$,

再把字符串反转,预处理新的字符串的长度为 $i$ 的前缀子串的哈希值 $suf[i]$,

这样,如果在原串中存在一个 $[l_1,r_1]$ 的回文串,那么对应到新串,这个区间就是 $[l_2,r_2] = [len - r_1 +1,len - l_1 + 1]$,这两个子串的哈希值应当是相等的,即:

$pre[r_1 ] - pre[l_1 - 1] \times P^x = suf[r_2 ] - suf[l_2 - 1] \times P^x$

其中,$x = r_1 - l_1 + 1 = r_2 - l_2 +1$。

所以,不难想到,我们如果二分回文子串的长度,就可以 $O\left( {\left| S \right|\log \left| S \right|} \right)$ 求出最长回文子串。

但是,这样做的话,在测样例时就会发现问题,奇数长度的回文子串和偶数长度的回文子串应当是分开计算的(因为回文串两侧同时各去掉一个字符,依然是回文串,且不改变奇偶性),

所以,需要两次二分,一次二分求得长度为偶数的最长回文子串(的长度),再一次二分求得长度为奇数的最长回文子串(的长度)。

最后输出两者中大的即可。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef unsigned long long ull;

const int P=131;
const int maxn=1000000+10;

int len;
char s[maxn];
int q;

ull pre[maxn],suf[maxn],Ppow[maxn];
void pretreat()
{
    pre[0]=0;
    suf[0]=0;
    Ppow[0]=1;
    for(int i=1;i<=len;i++)
    {
        pre[i]=pre[i-1]*P+(s[i]-'a'+1);
        suf[i]=suf[i-1]*P+(s[len-i+1]-'a'+1);
        Ppow[i]=Ppow[i-1]*P;
    }
}

bool found(int x)
{
    for(int l1=1;l1+x-1<=len;l1++)
    {
        int r1=l1+x-1;
        int r2=len-l1+1,l2=r2-x+1;
        ull A=pre[r1]-pre[l1-1]*(Ppow[x]);
        ull B=suf[r2]-suf[l2-1]*(Ppow[x]);
        if(A==B) return 1;
    }
    return 0;
}

int main()
{
    int kase=0;
    while(scanf("%s",s+1) && s[1]!='E')
    {
        len=strlen(s+1);
        pretreat();

        int l=1,r=len/2,mid;
        while(l<r)
        {
            mid=(l+r)/2+1;
            if(found(2*mid)) l=mid;
            else r=mid-1;
        }
        int ans1=l*2;

        l=0,r=(len-1)/2;
        while(l<r)
        {
            mid=(l+r)/2+1;
            if(found(2*mid+1)) l=mid;
            else r=mid-1;
        }
        int ans2=l*2+1;

        printf("Case %d: %d\n",++kase,max(ans1,ans2));
    }
}

猜你喜欢

转载自www.cnblogs.com/dilthey/p/9694249.html