Manacher(输出最长回文串及下标)

http://acm.hdu.edu.cn/showproblem.php?pid=3294

Girls‘ research

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 5711    Accepted Submission(s): 2117

Problem Description

One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
First
step: girls will write a long string (only contains lower case) on the
paper. For example, "abcde", but ‘a‘ inside is not the real ‘a‘, that
means if we define the ‘b‘ is the real ‘a‘, then we can infer that ‘c‘
is the real ‘b‘, ‘d‘ is the real ‘c‘ ……, ‘a‘ is the real ‘z‘. According
to this, string "abcde" changes to "bcdef".
Second step: girls will
find out the longest palindromic string in the given string, the length
of palindromic string must be equal or more than 2.

Input

Input contains multiple cases.
Each
case contains two parts, a character and a string, they are separated
by one space, the character representing the real ‘a‘ is and the length
of the string will not exceed 200000.All input must be lowercase.
If the length of string is len, it is marked from 0 to len-1.

Output

Please execute the operation following the two steps.
If
you find one, output the start position and end position of palindromic
string in a line, next line output the real palindromic string, or
output "No solution!".
If there are several answers available, please choose the string which first appears.

Sample Input

b babd
a abcd

Sample Output

0 2
aza
No solution!

Author

wangjing1111

Source

2010 “HDU-Sailormoon” Programming Contest

Recommend

lcy

题意:先转换,再输出最长回文串及下标。

思路:利用原数组与加工后的数组的数量关系得到下标。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#define INF  10000000
using namespace std;
char a[200009] , b[400009] , str[200009];
int p[400009];

int num;

int main()
{
    char c ;
    while(~scanf("%c%s" , &c , a))
    {
        int len = strlen(a);
        int q = c - ‘a‘;
        memset(str , 0 , sizeof(str));
        memset(p , 0 , sizeof(p));
        for(int i = 0 ; i < len ; i++)
        {
            if(a[i] - q < ‘a‘)
            {
                a[i] = a[i] + 26;
            }
            a[i] = a[i] - q ;
        }
        //printf("%s\n" , a);
        int l = 0 ;
        b[l++] = ‘$‘;
        b[l++] = ‘#‘;
        for(int i = 0 ; i < len ; i++)
        {
            b[l++] = a[i];
            b[l++] = ‘#‘;
        }
        int mx = - 1 , mid , ans = 0;
        for(int i = 1 ; i < l ; i++)
        {
            if(mx > i)
            {
                p[i] = min(p[2*mid-i] , mx - i);
            }
            else
            {
                p[i] = 1 ;
            }
            while(b[i-p[i]] == b[i+p[i]])
            {
                p[i]++;
            }
            if(mx < p[i]+i)
            {
                mid = i ;
                mx = p[i] + i;
            }
        }
        int index = 0  , indey = 0;
        for(int i = 0 ; i < l ; i++)
        {
            if(ans < p[i] - 1)
            {
                ans = p[i] - 1;
                index = i ;

            }
        }
        if(ans == 1)
        {
            printf("No solution!\n");
        }
        else
        {
            int r = (index + ans)/2 - 1;
            int l = r - ans + 1;
            printf("%d %d\n" , l , r);
            for(int i = l ; i <= r ; i++)
            {
                printf("%c" , a[i]);
            }
            printf("\n");

        }
        getchar();
    }

    return 0 ;
}

原文地址:https://www.cnblogs.com/nonames/p/11296274.html

时间: 2024-08-29 15:50:54

Manacher(输出最长回文串及下标)的相关文章

Manacher算法 最长回文串

Manacher算法O(n) 因为对于偶回文,是需要从虚轴扩充,ab,ba,所以如下: 先把原字符串处理,都加上一个标记符,比如#(特殊字符任何都可以,对于计算结果不会有影响) 1221-->#1#2#2#1# 121-->#1#2#1# 按照处理后的字符串求它的最长回文串长度m,所以原始字符串最长子回文串的长度是m/2 变量: 1:PArra[] 存放回文半径:某个位置能扩充的回文半径的长度,例如 #1#2#2#1#,2位置PArra[3] = 4 2:int PR 能够扫到的最右的回文的位

Manacher算法—最长回文串

若字符串长度为n,则算法的时间复杂度为o(n) 假设有一个字符串abaaba 先把该字符串变成$  #  a  #  b  #  a  #  a  #  b  #  a  # 第一个字符设为'$',防止计算的时候数组越界 再计算p数组,先给出p数组的答案 i为坐标,ma数组放改变后的字符串,p数组代表以该字符为中心,向右和向左延伸p[i]个,是回文串 i      0  1  2  3  4  5  6  7  8  9  10 11 12 13ma[]   $  #  a  #  b  # 

hdu 3068 最长回文串 o(n) Manacher 算法

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

HDU ACM 4513 吉哥系列故事——完美队形II-&gt;求最长回文串(manacher算法)

分析:该題可以通过求最长回文串的方法来解决:求最长回文串使用manacher算法,O(n)时间复杂度. 注意:while(a[i-len[i]]==a[i+len[i]] && a[i-len[i]]<=a[i-len[i]+2])这里多出的判断a[i-len[i]]<=a[i-len[i]+2]即为该題的限制从左到中保证身高不降,因在回文串的计算过程中添加了额外的字符,所以这里是i-len[i]+2而不是i-len[i]+1,以避开添加的字符. #include<ios

Manacher算法 O(n)求最长回文串

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

Hdu 3294 Girls&#39; research (manacher 最长回文串)

题目链接: Hdu 3294  Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.......,a == z. 问最长回文串所在区间,以及最长回文串所表示的明码. 解题思路: 字符串长度[1,200000],用manacher算法很轻松就搞定了. get√新技能请点击me 1 #include <cstdio> 2 #include <cstring> 3 #includ

(Manacher Algorithm, 中心拓展法,动态规划) leetcode 5. 最长回文串

解法一:中心拓展法.从下标为0开始遍历,将每个元素当作回文串中心,向两边拓展. 1)以这个字符为中心的回文串的长度(奇数串): 2)以这个字符和下个字符为中心的回文串的长度(偶数串). 注意:既要统计回文串为奇数时,又要统计回文串为偶数时.当 s[left]!=s[right] 时,left多减了1,right多加了1,所以在计算回文串开头时要把left+1,长度要是(right-1)-(left+1)-1 = right - left -1 class Solution { public: s

Manacher求最长回文

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

1112个人赛,最长回文串常见算法讨论

ps.此贴大部分文字与代码来自网上,我只是取长补短整理了下 S=“c a b a”  那么  S' = “a b a c”, 这样的情况下 S和 S‘的最长公共子串是aba.没有错误. 但是当 S=“abacdfgdcaba”, 那么S’ = “abacdgfdcaba”. 这样S和S‘的最长公共子串是abacd.很明显abacd并不是S的最长回文子串,它甚至连回文都不是. 现在是不是都明白为什么最长回文子串不能转化成为最长公共子串问题了.当原串S中含有一个非回文的串的反序串的时候,最长公共子串