POJ3974 Palindrome

Time Limit: 15000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

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
END

Sample Output

Case 1: 13
Case 2: 6

Source

Seventh ACM Egyptian National Programming Contest

manacher

半夜刷题太容易犯蠢了……Case打成case,又WA掉了十五分钟人生

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=1000100;
 9 char s[mxn],c[mxn<<1];
10 int len;
11 int p[mxn<<1];
12 void mana(){
13     int id=0,mx=0;
14     int i,j;
15     for(i=1;i<len;i++){
16         if(mx>i)p[i]=min(p[2*id-i],p[id]+id-i);
17         else p[i]=1;
18         while(c[i+p[i]]==c[i-p[i]])p[i]++;
19         if(p[i]+i>mx){
20             mx=p[i]+i;
21             id=i;
22         }
23     }
24     return;
25 }
26 int main(){
27     int cas=0;
28     while(scanf("%s",s)!=EOF){
29         if(s[0]==‘E‘)break;
30         int i,j;
31         c[0]=‘$‘;c[1]=‘#‘;
32         len=strlen(s);
33         for(i=0;i<len;i++){
34           c[i*2+2]=s[i];c[i*2+3]=‘#‘;
35         }
36
37         len=len*2+2;
38         c[len]=‘%‘;
39         mana();
40         int ans=0;
41         for(i=0;i<len;i++)ans=max(ans,p[i]);
42         printf("Case %d: %d\n",++cas,ans-1);
43     }
44     return 0;
45 }
时间: 2024-11-05 11:10:25

POJ3974 Palindrome的相关文章

POJ----(3974 )Palindrome [最长回文串]

Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 5121   Accepted: 1834 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 eff

poj3974 Palindrome【回文】【Hash】

Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 13157   Accepted: 5028 Description Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you pr

【Manacher算法】poj3974 Palindrome

Manacher算法教程:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 模板题,Code 附带注释: 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 char b[10000001],a[10000001]; 6 char tmp[10000001]; 7 int n,f[100

[poj3974] Palindrome 解题报告 (hash\manacher)

题目链接:http://poj.org/problem?id=3974 题目: 多组询问,每组给出一个字符串,求该字符串最长回文串的长度 数据范围支持$O(nlog n)$ 解法一: 二分+hash 回文串分奇数串和偶数串.对于奇数串,我们枚举它的中点,二分一下这个中点可以向两边扩展多远的距离:对于偶数串,我们枚举它中间两个点靠左的点,同样二分可以扩展的距离,这样时间复杂度就是$O(nlog n)$的了 说起来容易,写起来不是很容易 解法二: 每次跑一遍manacher就好了 说起来容易,写起来

POJ--3974 Palindrome(回文串,hash)

链接:点击这里 #include<iostream> #include<algorithm> #include<stdio.h> #include<cstring> using namespace std; #define maxn 1000005 #define LL long long #define ull unsigned long long const LL P = 131; ull p[maxn+10],f[maxn],ff[maxn]; int

manachar算法小结

1.hdu--4513 吉哥系列故事--完美队形II http://acm.hdu.edu.cn/showproblem.php?pid=4513 题意:中文题不解释 思路:数字型的manachar算法.将模板中的比较字符改为比较数字就行了. AC代码: 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <algorithm> 5 #include <cmat

0x14 Hash

Hash表 [例题]POJ3349 Snowflake Snow Snowflakes 建立一个哈希表,将N片雪花依次插入. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 const int INF=0x3f3f3f3f; 8 cons

POJ3974:Palindrome——题解

http://poj.org/problem?id=3974 题目大意: 求最大回文子串长度. ———————————————————— 马拉车板子题. 马拉车讲解先割. (测试过如果写成函数的话会很慢(2000+ms),这么写是(200+ms),所以不美观就不美观吧). #include<cstdio> #include<cstring> #include<algorithm> #define N 1000010 using namespace std; int l,

poj3974 最长回文串 exkmp

Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 3695   Accepted: 1338 Description Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you pro