POJ 3974-Palindrome

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
Source

Seventh ACM Egyptian National Programming Contest
.
.
.
.
.
.

分析

最长回文子串,也是子串+长度类型的问题,此处使用字符串哈希+二分解决。
对字符串进行正序倒序两次哈希,之后二分长度,对每个长度len枚举子串的哈希值,判断相同位置的子串正逆序哈希值是否相同,相同则代表为回文串。
复杂度O(n*log(n))。
.
.
.
.
.

程序:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const unsigned long long zd=30007uLL;
unsigned long long h[1000001],p[1000001],xp[1000001];
char s[1000001];
int n;

unsigned long long askhash(int l,int r)
{
    if (l==0) return h[r];
    return h[r]-h[l-1]*xp[r-l+1];
}

unsigned long long askp(int l,int r)
{
    if (r==n-1) return p[l];
    return p[l]-p[r+1]*xp[r-l+1];
}


void inithash()
{
    h[0]=s[0];
    for(int i=1;i<n;i++)
        h[i]=h[i-1]*zd+s[i];
    p[n-1]=s[n-1];
    for(int i=n-2;i>=0;i--)
        p[i]=p[i+1]*zd+s[i];
}

bool check(int len)
{
    unsigned long long ht,pt;
    for (int i=0,l,r;i+len-1<n;i++)
    {
        l=i;
        r=i+len-1;
        ht=askhash(l,r);
        pt=askp(l,r);
        if (ht==pt) return true;
    }
    return false;
}

int main()
{
    xp[0]=1;
    for (int i=1;i<1000001;i++)
        xp[i]=xp[i-1]*zd;
    int tj=0;
    while (1)
    {
        cin>>s;
        if (s[0]=='E') break;
        n=strlen(s);
        for (int i=0;i<n;i++)
            s[i]=s[i]-'a'+1;
        inithash();
        vector<int> a,b;
        for(int i=1;i<=n;i++)
            if (i % 2) a.push_back(i); else b.push_back(i);
        int ans=0,l=0,r=a.size()-1,m;
        while (l<=r)
        {
            m=(l+r)>>1;
            if (check(a[m]))
            {
                l=m+1;
                ans=a[m];
            } else r=m-1;
        }
        l=0;
        r=b.size()-1;
        while (l<=r)
        {
            m=(l+r)>>1;
            if (check(b[m]))
            {
                l=m+1;
                ans=max(ans,b[m]);
            } else r=m-1;
        }
        tj++;
        cout<<"Case "<<tj<<':'<<' '<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/YYC-0304/p/9499892.html