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;
    }
    
  • 相关阅读:
    20190318-使用类做一个简单的图书馆管理系统
    20190313-面向对象的简单理解
    20190313-时间和日期-Time
    20190305-leetcode题目有效的括号
    20190227-做一个简易代码统计工具
    20190226-利用序列化完成小型记账程序
    20190221-文件操作
    20190131-文件操作命题练习
    Excel技巧—如何从重复行中取某一行
    Excel技巧—轻松搞定多级联动下拉列表
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/8414433.html
Copyright © 2011-2022 走看看