码迷,mamicode.com
首页 > 其他好文 > 详细

six solutions to a single symmetrical problem

时间:2014-04-29 17:22:46      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   art   for   

Problem description:

given a string, find the longest palindrome string in it

Solution:

1.brute force 

  O(n^3)

just enumerate start and end of the substring and using head pointer and tail pointer to judge if it‘s a palindrome

2.enumerate center

but I failed to think of even-length palindrome

3.dp

so sorry for this part...

dp[i][j](i <= j) = 1 if a[i]...a[j] is a substring

                     = 0 else

then dp[i][i] = 1 for all i

        dp[i][j] = (a[i] == a[j] && dp[i-1][j+1]) for j - i > 2;

        dp[i][i+1] = (a[i] == a[i+1]);

since the matrix is n^2 and each element is written only once, O(n^2)

4.using suffix trees

reducing to O(nlogn)

perhaps refer to wiki might offer a good solution

5.Manacher‘s Algorithm

http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html#comment-166993

6. using KMP algorithms

however,the solution is wrong since match part does not necessarily is a palindrome

but the point is worth thinking

http://blog.csdn.net/hopeztm/article/details/7932245

another problem:what if it aims to find the longest subsequence?

 

 

 

 

six solutions to a single symmetrical problem,码迷,mamicode.com

six solutions to a single symmetrical problem

标签:des   blog   http   io   art   for   

原文地址:http://www.cnblogs.com/warmfrog/p/3699637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!