hdu3608 manacher模板题

给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 
回文就是正反读都是一样的字符串,如aba, abba等

Input输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S 
两组case之间由空行隔开(该空行不用处理) 
字符串长度len <= 110000Output每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度. 
Sample Input

aaaa

abab

Sample Output

4
3题意:找最长回文子串题解:manacher算法,据说这是一道连后缀数组O(nlogn)也卡的神题。。结果真的是,第一次用manacher写也tle了,估计是写搓了改了好几遍

#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){
        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<<ans<<endl;
    }
    return 0;
}

时间: 2024-08-08 01:52:42

hdu3608 manacher模板题的相关文章

ural 1297 Palindrome(Manacher模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx?space=1&num=1297 Manacher模板题,复杂度O(n),做这题纯属是为了验一下自己写的模板是否正确. 当然这题也可以用后缀数组来搞 1 #include <iostream> 2 #include <sstream> 3 #include <ios&g

HDU 3068 最长回文(Manacher模板题)

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

hdu3068 最长回文 马拉车模板题

题目传送门 马拉车算法模板题. 学习博客 #include<bits/stdc++.h> #define clr(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; const int maxn=110010; char s[maxn<<1],ne[maxn<<1]; int p[maxn<<1],mx,maxx; int init(){ int len=strlen(s

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>

几道树剖模板题

寒假后半段一直都在外出旅游..颓了好久..qaq 旅游期间写了几道树剖模板题,贴上来.. BZOJ 1036 没啥好说的,裸题 1 #include <cstdio> 2 #include <algorithm> 3 4 #define LEFT (segt[cur].l) 5 #define RIGHT (segt[cur].r) 6 #define MID (segt[cur].mid) 7 #define SUM (segt[cur].Sum) 8 #define MAX (

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

洛谷P3381——费用流模板题

嗯..随便刷了一道费用流的模板题....来练练手. #include<iostream> #include<cstdio> #include<cstring> using namespace std; int h[5210],d[5210],used[5210],que[100010],last[5210]; int k=1,INF=0x7fffffff,ans1=0,ans2=0; inline int read(){ int t=1,num=0; char c=ge

HDU 1251 Trie树模板题

1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b