Leetcode 之 Longest Palindrome

#include <cstdio>
#include <algorithm>

int MAX_SIZE = 100000;

int gen_odd_str(const char * str, char * odd_str) {
    int i = 0;
    for (i = 0; *str != 0 ; ++i, ++str) {
        odd_str[2 * i] = ‘#‘;
        odd_str[2 * i + 1] = *str;
    }
    odd_str[2 * i] = ‘#‘;
    odd_str[2 * i + 1] = 0;
    return 2 * i + 1;
}

char* longestPalindrome(char* str) {
    using std::max;
    using std::min;
    char odd_str[MAX_SIZE];
    int p[MAX_SIZE], id, i, j, size;
    size = gen_odd_str(str, odd_str); p[0] = 1; id = 0;
    for (i = 1; i < size; ++i) {
        p[i] = max(1, min(p[id] - i, 2 * id - i > -1 ? p[2 * id - i] : 0));
        for (j = i - 1; j > -1 && odd_str[j] == odd_str[2 * i - j]; --j) ++p[i];
        if (p[i] > p[id]) id = i;
    }
    str = str + (id - p[id] + 1) / 2;
    str[p[id] - 1] = 0;
    return str;
}

int main(){
    char str[MAX_SIZE];
    while (!feof(stdin)) {
        gets(str);
        printf("%s", longestPalindrome(str));
    }
}

时间: 2024-08-29 23:03:59

Leetcode 之 Longest Palindrome的相关文章

LeetCode 409 Longest Palindrome

Problem: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Assume the l

LeetCode——409. Longest Palindrome

题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note: Assume the

LeetCode #5 Longest Palindrome

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

LeetCode the longest palindrome substring

回文检测,参考http://blog.csdn.net/feliciafay/article/details/16984031 使用时间复杂度和空间复杂度相对较低的动态规划法来检测,具体的做法 图一 偶数个回文字符情况 图二 奇数个回文字符情况 核心就是如果一个子串是回文,如果分别向回文左右侧扩展一个字符相同,那么回文就向外扩展一位. 实现的代码如下 bool table[1000][1000] = {false}; int sStart = 0; int iLength = 1; int n

LeetCode Longest Palindrome

原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa&qu

[LeetCode] Longest Palindrome 最长回文串

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note: Assume the leng

[LeetCode&amp;Python] Problem 409. Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note:Assume the lengt

409. Longest Palindrome

题目: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here. Note:Assume the l

[LeetCode] 125. Valid Palindrome 有效回文

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th