LeetCode - 5. Longest Palindromic Substring

简介: 5. Longest Palindromic Substring  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个字符串,输出这个字符串的最长回文子串.

5. Longest Palindromic Substring 

Problem's Link

 ----------------------------------------------------------------------------

Mean: 

给定一个字符串,输出这个字符串的最长回文子串.

analyse:

水题.

使用Manacher算法求Ma和Mp数组,根据这两个数组来构造最长回文子串即可.

Time complexity: O(N)

 

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2015 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2015-12-10-11.04
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long( LL);
typedef unsigned long long( ULL);
const double eps( 1e-8);


/*
Given a string S, find the longest palindromic substring in S.
You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
*/

class Solution
{
public :
    /** O(n)内求出所有回文串
    *原串 :abaaba
    *Ma串 :.,a,b,a,a,b,a,
    *Mp[i]:Ma串中,以字符Ma[i]为中心的最长回文子串的半径长度(包括Ma[i],也就是把回文串对折后的长度).
    ****经过对原串扩展处理后,将奇数串的情况也合并到了偶数的情况(不需要考虑奇数串)
    */
    const static int MAXN = 1010;
    char Ma [ MAXN * 2 ],s [ MAXN ];
    int Mp [ MAXN * 2 ], Mplen;
    void Manacher( string s)
    {
          memset( Ma , '\0' , sizeof Ma);
          int len =s . length();
          int le = 0;
          Ma [ le ++ ] = '.';
          Ma [ le ++ ] = ',';
          for( int i = 0; i < len; ++ i)
          {
                Ma [ le ++ ] =s . at( i);
                Ma [ le ++ ] = ',';
          }
          Mplen = le;
          Ma [ le ] = 0;
          int pnow = 0 , pid = 0;
          for( int i = 1; i < le; ++ i)
          {
                if( pnow > i)
                      Mp [ i ] = min( Mp [ 2 * pid - i ], pnow - i);
                else
                      Mp [ i ] = 1;
                for(; Ma [ i - Mp [ i ]] == Ma [ i + Mp [ i ]]; ++ Mp [ i ]);
                if( i + Mp [ i ] > pnow)
                {
                      pnow = i + Mp [ i ];
                      pid = i;
                }
          }
    }

    string longestPalindrome( string s)
    {
        int idx = 0;
        int MaxPalindomLength = 1;
        Manacher(s);
        for( int i = 0; i < Mplen; ++ i)
        {
            if( Mp [ i ] > MaxPalindomLength)
                MaxPalindomLength = Mp [ i ], idx = i;
        }
        -- MaxPalindomLength;
        int startPos = idx - MaxPalindomLength + 1;
        char str [ MAXN ];
        int strLen = 0;
        for( int i = startPos; strLen < MaxPalindomLength; i += 2)
            str [ strLen ++ ] = Ma [ i ];
        str [ strLen ] = '\0';
        return string( str);
    }
};

int main()
{
    Solution solution;
    string s;
    while( cin >>s)
    {
        cout << solution . longestPalindrome(s) << endl;
    }
    return 0;
}
目录
相关文章
Leetcode 516. Longest Palindromic Subsequence
找到一个字符串的最长回文子序列,这里注意回文子串和回文序列的区别。子序列不要求连续,子串(substring)是要求连续的。leetcode 5. Longest Palindromic Substring就是求连续子串的。
63 0
|
Java
Leetcode 3. Longest Substring Without Repeating Characters
此题题意是找出一个不包含相同字母的最长子串,题目给出了两个例子来说明题意,但这两个例子具有误导性,让你误以为字符串中只有小写字母。还好我是心机boy,我把大写字母的情况也给考虑进去了,不过。。。。字符串里竟然有特殊字符,于是贡献了一次wrong answer,这次我把ascii字符表都考虑进去,然后就没问题了。这个故事告诫我们在编程处理问题的时候一定要注意输入数据的范围,面试中可以和面试官去确认数据范围,也能显得你比较严谨。
68 3
LeetCode 424. Longest Repeating Character Replacem
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次。在执行上述操作后,找到包含重复字母的最长子串的长度。
115 0
LeetCode 424. Longest Repeating Character Replacem
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
96 0
LeetCode 409. Longest Palindrome
LeetCode 395. Longest Substring with At Least K
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k 。输出 T 的长度。
117 0
LeetCode 395. Longest Substring with At Least K
|
Windows
LeetCode 388. Longest Absolute File Path
我们致力于寻找我们文件系统中文件的最长 (按字符的数量统计) 绝对路径。例如,在上述的第二个例子中,最长路径为 "dir/subdir2/subsubdir2/file2.ext",其长度为 32 (不包含双引号)。
97 0
LeetCode 388. Longest Absolute File Path
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
88 0
LeetCode 329. Longest Increasing Path in a Matrix
|
6月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
7月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
79 6
|
7月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
152 2