程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj3974之manacher算法

poj3974之manacher算法

編輯:C++入門知識

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 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 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;
}

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved