poj3974 Palindrome(hash,二分)

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

题意:求最大回文子串。

分析:枚举一个字符串中点,二分子串长度,hash判断前后俩个部分是否相同,那么如何hash呢?一般取P=131,把字符串转换为该进制数对M取余,这里我们直接定义unsigned long long自然溢出,即取M=2^64,若h[i]为1~i的子串的hash值,那么l~r的子串hash值为h[r]-h[l]*P^(r-l+1),所以只要求出前缀子串的hash值就可以O(1)求出任意子串的hash值,当hash值相等,我们认为对应子串相等。注意奇数子串要与偶数子串分开求。

代码

#include <cstdio>
#include <string>
#include <cstring>
#define N 1000005
#define ull unsigned long long
using namespace std;

char s[N];
ull f1[N],f2[N],p[N];

int main()
{
    p[0] = 1;
    for (int i = 1; i <= 1000000; i++)  p[i] = p[i-1] * 131;
    int cnt = 0;
    while (~scanf("%s", s + 1))
    {
        int max = 0;
        if (!strcmp(s + 1 , "END")) break;
        cnt++;
        printf("Case %d: ", cnt);
        int n = strlen(s+1);
        for (int i = 1; i <= n; i++)
            f1[i] = f1[i - 1] * 131 + (s[i] - 'a' + 1);
        f2[n + 1] = 0;
        for (int i = n; i >= 1; i--)
            f2[i] = f2[i + 1] * 131 + (s[i] - 'a' + 1);
        int ans = 0;
        for (int i = 1; i < n; i++)
        {
            int l = 0, r = n / 2;
            while (l <= r)
            {
                int mid = (l + r) / 2;
                int l1 = i - mid;
                int r1 = i + mid;
                if (l1 <= 0 || r1 > n) 
                {
                    r = mid - 1;
                    continue;
                }
                if (f1[i - 1] - f1[l1 - 1] * p[i - l1] == f2[i + 1] - f2[r1 + 1] * p[r1 - i])
                {
                    ans = mid;
                    l = mid + 1;    
                }else r = mid - 1;
            }
            if (2 * ans + 1 > max) max = 2 * ans + 1;
            ans = 0;
            l = 0; r = n / 2;
            while (l <= r)
            {
                int mid = (l + r) / 2;
                int l1 = i - mid + 1;
                int r1 = i + mid;
                if (l1 <= 0 || r1 > n) 
                {
                    r = mid - 1;
                    continue;
                }
                if (f1[i] - f1[l1 - 1] * p[i - l1 + 1] == f2[i + 1] - f2[r1 + 1] * p[r1 - i])
                {
                    ans = mid;
                    l = mid + 1;    
                }else r = mid - 1;
            }
            if (2 * ans > max) max = 2 * ans;
        }
        printf("%d\n", max);
    }
}

猜你喜欢

转载自blog.csdn.net/zhanghaoxian1/article/details/81741767
今日推荐