【问题标题】:Which is the best algorithm to find a longest palindrome in a given string? [duplicate]在给定字符串中找到最长回文的最佳算法是什么? [复制]
【发布时间】:2012-05-07 14:31:17
【问题描述】:

可能重复:
Write a function that returns the longest palindrome in a given string

给定长度为“n”的字符串,我需要最长的回文,其时间和空间复杂度应该是有效的。

任何人都可以帮助我至少使用伪代码吗?

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 您希望回文使用连续字符吗?即您认为 ABCBA 包含在 ABBCBDA 中吗?

标签: algorithm palindrome


【解决方案1】:

一种方法是将字符串中的每个字符视为回文的可能“中心”,然后左右扩展,只要左侧位置的字符等于右侧位置的字符(这是一个要求子串是回文,显然要考虑子串奇偶长度的两种情况)。对源字符串中的所有位置 1..n 执行此操作将为您提供字符串中包含的连续字符中最长的回文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-28
    • 2017-11-23
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多