hihocoder1032(最长回文子串manacher算法)

题目连接:点击打开链接

解题思路:

manacher算法的模板题。

完整代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <complex>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
using namespace std;
typedef unsigned long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); //M_PI;
const int maxn = 2000001;
int n;
char s[maxn] , t[maxn];
int len[maxn];

void manacher(char t[])
{
    int t_len = strlen(t);
    int mx = 0 , id;
    for(int i = 1 ; i < t_len ; i ++)
    {
        if(mx > i)  len[i] = min(len[id * 2 - i] , mx - i);
        else    len[i] = 1;
        while(t[i + len[i]] == t[i - len[i]])     len[i] ++;
        if(len[i] + i > mx)
        {
            mx = len[i] + i;
            id = i;
        }
    }
}

void solve(char s[])
{
    int s_len = strlen(s);
    int t_len = s_len * 2 + 2;
    t[0] = '+';
    for(int i = 0 ; i < t_len ; i ++)
    {
        t[i * 2 + 1] = '#';
        t[i * 2 + 2] = s[i];
    }
    manacher(t);
    int ans = -INF;
    for(int i = 1 ; i < t_len ; i ++)
        ans = max(ans , len[i]);
    printf("%d\n" , ans - 1);
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    while(~scanf("%d",&n))
    {
        for(int i = 0 ; i < n ; i ++)
        {
            scanf("%s" , s);
            solve(s);
        }
    }
    return 0;
}
时间: 2024-08-04 01:52:11

hihocoder1032(最长回文子串manacher算法)的相关文章

HiHo 1032 最长回文子串 (Manacher算法求解)

Manacher算法o(n)求解最长回文子串问题 非常巧妙 #include<bits/stdc++.h> using namespace std; char str[2000020],s[2000020]; int p[2000020]; int len,id,mx; void pre() //对字符串进行预处理 { len=strlen(s); str[0]='$'; str[1]='#'; for(int i=0;i<len;i++) { str[i*2+2]=s[i]; str[

hiho#1032 : 最长回文子串 (manacher算法O(n)时间求字符串的最长回文子串 )

#1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:"小Ho,你能不能分别在这些字符串中找到它们每一个的最长回文子串呢?" 小Ho奇怪的问道:"什么叫做最长回文子串呢?" 小Hi回答道:"一个字符串中连续的一

hihoCoder #1032 : 最长回文子串 [ Manacher算法--O(n)回文子串算法 ]

传送门 #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能分别在这些字符串中找到它们每一个的最长回文子串呢?” 小Ho奇怪的问道:“什么叫做最长回文子串呢?” 小Hi回答道:“一个字符串中连续的一段就是这个字符串的子串,而回文串

最长回文子串 - Manacher算法

算法思想: 设有字符串s[] = "121" 第一步:通过在每个字符左右都添加一个特殊字符,把奇数长度和偶数长度的字符串都转化成奇数(例如. "121" 加上特殊字符后变成"#1#2#1" ),同时也可在开头再加一个特殊字符,以便于忽略越界问题(如上例"121"变成"$#1#2#1#"  此时开头的特殊字符$和字符串末尾的\0与此串中其他字符都不同,即可忽略越界问题),此时字符串变成 s[] = "

最长回文子串Manacher算法模板

Manacher算法能够在O(N)的时间复杂度内得到一个字符串以任意位置为中心的回文子串.其算法的基本原理就是利用已知回文串的左半部分来推导右半部分. 例题:HDU 3068 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 using namespace std; 5 const int N=110005; 6 char s[N],cpy[N<<1]; 7 int rad[N&l

求最长回文子串——Manacher算法

回文串包括奇数长的和偶数长的,一般求的时候都要分情况讨论,这个算法做了个简单的处理把奇偶情况统一了.算法的基本思路是这样的,把原串每个字符中间用一个串中没出现过的字符分隔开来(统一奇偶),用一个数组p[ i ]记录以 str[ i ] 为中间字符的回文串向右能匹配的长度.先看个例子 原串:       w  a   a   b   w   s   w   f   d 新串(str):  #   w  #   a   #   a   #   b  #   w   #   s    #   w   

HDU3068(最长回文子串manacher算法)

题目连接:点击打开链接 解题思路: manacher算法模板题. 完整代码: #include <algorithm> #include <iostream> #include <cstring> #include <complex> #include <cstdio> #include <string> #include <cmath> #include <queue> using namespace std

5. Longest Palindromic Substring(最长回文子串 manacher 算法)

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example: Input: "cbbd" Ou

【最长回文子串】HDU3068最长回文【Manacher算法】

一张图领悟Manacher算法,计算字符串最长回文子串 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S 两组case之间由空行隔开(该空行不用处理)