OJ练习44——T5 Longest Palindormic Substring

求字符串的最长回文子串。

【思路】

1.从两边开始向中间发展

2.从中间开始向两边发展

3.从中间开始的变体,较为复杂,详见http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

【other code1-思路2】

 string longestPalindrome(string s) {
        int n=s.size();
        if(n==0) return "";
        string re=s.substr(0,1);
        for(int i=0; i<n; i++){
            string p1=testPalindrome(s,i,i);
            if(p1.size()>re.size())
                re=p1;
            string p2=testPalindrome(s,i,i+1);
            if(p2.size()>re.size())
                re=p2;
        }
        return re;
    }
    string testPalindrome(string s, int c1, int c2){
        int l=c1;
        int r=c2;
        int n=s.size();
        while(l>=0&&r<=n-1&&s[l]==s[r]){
            l--;
            r++;
        }
        return s.substr(l+1,r-l-1);
    }

【结果1】

时间是O(n^2),100+ms,排名靠后。

【other code2-思路3】

// Transform S into T.
// For example, S = "abba", T = "^#a#b#b#a#$".
// ^ and $ signs are sentinels appended to each end to avoid bounds checking
string preProcess(string s) {
  int n = s.length();
  if (n == 0) return "^$";
  string ret = "^";
  for (int i = 0; i < n; i++)
    ret += "#" + s.substr(i, 1);

  ret += "#$";
  return ret;
}

string longestPalindrome(string s) {
  string T = preProcess(s);
  int n = T.length();
  int *P = new int[n];
  int C = 0, R = 0;
  for (int i = 1; i < n-1; i++) {
    int i_mirror = 2*C-i; // equals to i‘ = C - (i-C)

    P[i] = (R > i) ? min(R-i, P[i_mirror]) : 0;

    // Attempt to expand palindrome centered at i
    while (T[i + 1 + P[i]] == T[i - 1 - P[i]])
      P[i]++;

    // If palindrome centered at i expand past R,
    // adjust center based on expanded palindrome.
    if (i + P[i] > R) {
      C = i;
      R = i + P[i];
    }
  }

  // Find the maximum element in P.
  int maxLen = 0;
  int centerIndex = 0;
  for (int i = 1; i < n-1; i++) {
    if (P[i] > maxLen) {
      maxLen = P[i];
      centerIndex = i;
    }
  }
  delete[] P;

  return s.substr((centerIndex - 1 - maxLen)/2, maxLen);
}

【结果2】

时间是O(n), 12ms,排名第一

为毛中档的题目就这么难!  (? •?_•?)?┻━┻

下午分析。

时间: 2024-11-13 01:32:50

OJ练习44——T5 Longest Palindormic Substring的相关文章

LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

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. 玩了两天dota2,罪过罪过,还是应该老老实实刷题啊. 题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以

【LeetCode OJ】Longest Palindromic Substring

题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目: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. 解题思路:p

hdu1403 Longest Common Substring

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1403 题目: Longest Common Substring Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6296    Accepted Submission(s): 2249 Problem Description Give

ZOJ 3199 Longest Repeated Substring

Longest Repeated Substring Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 319964-bit integer IO format: %lld      Java class name: Main Write a program that takes a string and returns length of the longest r

[译]最长回文子串(Longest Palindromic Substring) Part II

[译+改]最长回文子串(Longest Palindromic Substring) Part II 问题:给定字符串S,求S中的最长回文子串. 在上一篇,我们给出了4种算法,其中包括一个O(N2)时间O(1)空间的算法(中心检测法),已经很不错了.本篇将讨论一个O(N)时间O(N)空间的算法,即著名的Manacher算法,并详细说明其时间复杂度为何是O(N). 提示 +BIT祝威+悄悄在此留下版了个权的信息说: 先想想有什么办法能改进中心检测法. 考虑一下最坏的情况.★ 最坏的情况就是各个回文

后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the set of lowercase letters. Substring, also called factor, is a consecutive sequence of characters occurrences at

Longest Palindromic Substring - 字符串中最长的回文字段

需求: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. 如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string 判断回文数,中间开花.定一个中心,向两边散

[LeetCode][Python]Longest Palindromic Substring

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000,and there exis

spoj1812 LCS2 - Longest Common Substring II

地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the set of lowercase letters. Substring, also called factor, is a cons