当前位置:编程学习 > C/C++ >>

poj3974之manacher算法

Time Limit: 15000MS   Memory Limit: 65536K 
Total Submissions: 2596   Accepted: 948 
 
 
Description
 
Andy the smart computer science student was attending an algorithms class when the professor asked the students a 易做图 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
ENDSample Output
 
Case 1: 13
Case 2: 6
#include<iostream>   
#include<cstdio>   
#include<cstdlib>   
#include<cstring>   
#include<string>   
#include<queue>   
#include<algorithm>   
#include<map>   
#include<iomanip>   
#define INF 9999999   
using namespace std;  
  
const int MAX=1000000+10;  
char s[MAX*2];  
int p[MAX*2];  
  
int main(){  
    int num=0;  
    while(scanf("%s",s),s[0] != 'E'){  
        int len=strlen(s);  
        for(int i=len;i>=0;--i){  
            s[i+i+2]=s[i];  
            s[i+i+1]='#';  
        }  
        s[0]='*';  
        int k=1,maxlen=0;  
        for(int i=2;i<len+len+1;++i){  
            int maxr=k+p[k]-1;  
            p[i]=min(p[2*k-i],max(maxr-i+1,1));  
            while(s[i-p[i]] == s[i+p[i]])++p[i];  
            if(i+p[i]>k+p[k])k=i;  
            if(p[i]>maxlen)maxlen=p[i];  
        }  
        cout<<"Case "<<++num<<": "<<maxlen-1<<endl;  
    }  
    return 0;  
}  

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 9999999
using namespace std;

const int MAX=1000000+10;
char s[MAX*2];
int p[MAX*2];

int main(){
	int num=0;
	while(scanf("%s",s),s[0] != 'E'){
		int len=strlen(s);
		for(int i=len;i>=0;--i){
			s[i+i+2]=s[i];
			s[i+i+1]='#';
		}
		s[0]='*';
		int k=1,maxlen=0;
		for(int i=2;i<len+len+1;++i){
			int maxr=k+p[k]-1;
			p[i]=min(p[2*k-i],max(maxr-i+1,1));
			while(s[i-p[i]] == s[i+p[i]])++p[i];
			if(i+p[i]>k+p[k])k=i;
			if(p[i]>maxlen)maxlen=p[i];
		}
		cout<<"Case "<<++num<<": "<<maxlen-1<<endl;
	}
	return 0;
}

 


补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,