poj3974 manacher

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: 6manacher模板没啥好说的。(有一点要注意就是数组要开两倍大,不然就会runtime error)

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 10007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1

using namespace std;

const double g=10.0,eps=1e-9;
const int N=2000000+5,maxn=(1<<18)-1,inf=0x3f3f3f3f;

string str;
int p[N],slen;

void manacher()
{
    int mx=0,id;
    for(int i=1;i<slen;i++)
    {
        if(mx>i)p[i]=min(p[2*id-i],mx-i);
        else p[i]=1;
        while(str[i+p[i]]==str[i-p[i]])p[i]++;
        if(p[i]+i>mx)
        {
            mx=p[i]+i;
            id=i;
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
 //   cout<<setiosflags(ios::fixed)<<setprecision(2);
    string s;
    int cnt=0;
    while(cin>>s){
        if(s=="END")break;
        str="$#";
        for(int i=0;i<s.size();i++)
        {
            str+=s[i];
            str+="#";
        }
        slen=str.size();
        memset(p,0,sizeof p);
        manacher();
        int ans=-1;
        for(int i=1;i<str.size();i++)
            ans=max(ans,p[i]-1);
       cout<<"Case "<<++cnt<<": "<<ans<<endl;
    }
    return 0;
}

时间: 2024-10-12 03:19:07

poj3974 manacher的相关文章

poj3974(manacher)

传送门:Palindrome 题意:给定一个字符串,求最长回文子串. 分析:manach裸题,核心理解mx>i?p[i]=min(p[2*id-i],mx-i):1. #pragma comment(linker,"/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <string> #include <cmath> #includ

【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就好了 说起来容易,写起来

Manacher算法--Poj3974

https://subetter.com/algorithm/manacher-algorithm.html #include<bits/stdc++.h> #define N 2050001 using namespace std; int len,p[N],Case,ans; char ch[N],s[N*2]; void manacher() { int cnt=1;s[0]='%';s[1]='#'; for(int i=0;i<len;i++) { s[++cnt]=ch[i]

Hdu 3068 最长回文字串Manacher算法

题目链接 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7976    Accepted Submission(s): 2735 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输入

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

【学习笔记】Manacher

扔板子跑路 代码 POJ3974 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s[1100009],now[2200003]; int p[2200003]; int Manacher() { memset(p,0,sizeof(p)); int len=strlen(s+1); for(int i=1;i<=len;i++) { now[2

BZOJ 2084 [Poi2010]Antisymmetry(manacher)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2084 [题目大意] 对于一个01字符串,如果将这个字符串0和1取反后, 再将整个串反过来和原串一样,就称作“反对称”字符串. 比如00001111和010101就是反对称的,1001就不是. 现在给出一个长度为N的01字符串,求它有多少个子串是反对称的. [题解] 修改manacher的判定条件,对该串进行计算即可. [代码] #include <cstdio> #include

HDU 3068 最长回文 (manacher算法)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9188    Accepted Submission(s): 3159 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组