POJ - 3974 Palindrome 最长的回文子串 3种不同的方法 字符串hash/Manacher算法

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.暴力

暴力解法

2.字符串hash,分奇偶进行判断,先预处理前缀和子串然,后二分长度,对于一个子串,看他正面的hash和反转后的hash值是否一样。时间复杂度O(NlogN)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAXN=1e6+5;
const int MOD=3e4+7;
typedef long long ll;
ll h[MAXN],p[MAXN],xp[MAXN];
char s[MAXN];
int len;
void initHash()
{
    h[0]=s[0];
    for(int i=1;i=0;i--)
        p[i]=p[i+1]*MOD+s[i];
}
ll askh(int l,int r)
{
    if(l==0)
        return h[r];
    return h[r]-h[l-1]*xp[r-l+1];
}
ll askp(int l,int r)
{
    if(r==len-1)
        return p[l];
    return p[l]-p[r+1]*xp[r-l+1];
}
bool check(int n)
{
    ll ht,pt;
    int l,r;
    for(int i=0;i+n-1 d,v;
        for(int i=1;i<=len;i++){  //分长度为奇偶
            if(i%2)
                d.push_back(i);  //奇数
            else
                v.push_back(i);  //偶数
        }
        int ans=0,l=0,r=d.size()-1,m;
        while(l<=r){
            m=(l+r)>>1;
            if(check(d[m])){
                l=m+1;
                ans=d[m];
            }
            else
                r=m-1;
        }
        l=0,r=v.size()-1;
        while(l<=r){
            m=(l+r)>>1;
            if(check(v[m])){
                l=m+1;
                ans=max(ans,v[m]);
            }
            else
                r=m-1;
        }
        printf("Case %d: %d\n",++icase,ans);
    }
}

3.Manacher算法 时间复杂度O(N)即可解决

#include 
#include 
#include 
#include 
using namespace std;
 
char str[1000005];
int p[1000005<<1];
char a[1000005<<1];
int min(int a,int b){
	return a>b?b:a;
}
void result(){
	int maxLine=0,ID=1,maxResult=0;
	int n=0,i,len,lentmp;
	lentmp=strlen(str);
	len=lentmp<<1;
	
	for(i=0;i<(1000005<<1);i++){
		p[i]=0;
		a[i]='#';
	}
	for(i=0;ii){
			p[i]=min(p[2*ID-i],maxLine-i);
		}
		else{
			p[i]=1;
		}
		
		while(a[i+p[i]]==a[i-p[i]]){
			p[i]++;
 
		}
		if(p[i]+i>maxLine){
		   maxLine=p[i]+i;
		   ID=i;
		}
		if(p[i]>maxResult){
		   maxResult=p[i];	
		}
	}
 
 
	cout<>str;
        if(strcmp(str,"END")==0)
         return 1;
        else
        cout<<"Case "<

 

你可能感兴趣的:(ACM_字符串,POJ,ACM_hash)