zoukankan      html  css  js  c++  java
  • [POJ3974]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.InputYour 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).OutputFor each test case in the input print the test case number and the length of the largest palindrome.

    给出一个长度为N(1 <= N <= 1000000)的字符串,求最长回文子串的长度。

    Sample Input
    abcbabcbabcba
    abacacbaaaab
    END

    Sample Output
    Case 1: 13
    Case 2: 6


    裸的Manacher算法,没什么好讲的,如果不会Manacher算法点这里

    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline int read(){
    	int x=0,f=1;char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')    f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x>=10)     print(x/10);
    	putchar(x%10+'0');
    }
    const int N=2e6;
    char t[N+10],s[N+10];
    int p[N+10];
    int main(){
    	for (int Case=1;;Case++){
    		scanf("%s",t+1);
    		if (t[1]=='E'&&t[2]=='N'&&t[3]=='D')	break;
    		memset(p,0,sizeof(p));
    		memset(s,0,sizeof(s));
    		int len=strlen(t+1),Max=0,ID=0,Ans=0;
    		for (int i=1;i<=len;i++)	s[i<<1]=t[i],s[i<<1|1]='&';
    		len=len<<1|1;
    		s[1]='&',s[0]='%',s[len+1]='#';
    		for (int i=1;i<=len;i++){
    			p[i]=Max>i?min(p[ID*2-i],Max-i):1;
    			while (s[i+p[i]]==s[i-p[i]])	p[i]++;
    			if (Max<i+p[i])	Max=p[ID=i]+i;
    			if (Ans<p[i])	Ans=p[i];
    		}
    		printf("Case %d: %d
    ",Case,Ans-1);
    	}
    	return 0;
    }
    
  • 相关阅读:
    IT民工的时间哪儿来的
    解决Office2007安装时出现错误1706的方法
    情人节特献:有心之函数必然就有分手函数
    mathematica汉化 版本二
    项目经理职责与权利
    什么是产品经理?主要职责是什么?
    调查收藏
    如何在CLI命令行下运行PHP脚本,同时向PHP脚本传递参数?
    PHP的GC垃圾收集机制
    AWStats分析Tomcat\Apache\IIS\nginx 的访问日志
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/8414433.html
Copyright © 2011-2022 走看看