poj 3974 Palindrome(最长回文子串,处理大数,Manacher算法)

本文介绍使用Manacher算法高效求解最长回文子串的问题,针对长度可达100万的大规模输入字符串,提供了一种优于传统迭代方法的解决方案,并附带详细代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、http://poj.org/problem?id=3974

2、题目大意:

给定一串字符串,输出最长回文串的长度,此题难点在于字符串长度1000000,用二维dp会超时,在此用Manacher算法解决,见上一篇博客

Palindrome
Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 2225 Accepted: 801

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

 

3、代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1000005
char str[2*N+2];
char st[N];
int p[2*N+2];
int main()
{
    int cas=0;
    while(scanf("%s",st)!=EOF)
    {
        if(strcmp(st,"END")==0)
        break;
        cas++;
        int nn=strlen(st);
        int n=2*nn+2;
        str[0]='$';//str首位置赋值$
        for(int i=0;i<=nn;i++)
        {
            str[2*i+2]=st[i];
            str[2*i+1]='#';
        }//str从下标1开始
        int ans=-999;
        memset(p,0,sizeof(p));//p[i]数组记录以str[i]为中心的最长回文子串的长度,p[i]-1即为原来字符串的长度
        int mx=0;//mx用来记录p[i]中到达的最右端的值
        int id;//id用来记录p[i]延长至最右端的下标i
        for(int i=1;i<n;i++)
        {
            if(mx>i)
            p[i]=min(p[2*id-i],mx-i);//不太明白,待深思。。。。
            else
            p[i]=1;
            while(str[i-p[i]]==str[i+p[i]])
            p[i]++;
            if(mx<i+p[i])
            {
                mx=i+p[i];
                id=i;
            }
            if(ans<p[i])
            ans=p[i];
        }
        printf("Case %d: %d\n",cas,ans-1);//输出的ans-1,即是最长的回文串长度
    }
    return 0;
}
/*
abcbabcbabcba
abacacbaaaab
END
*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值