six solutions to a single symmetrical problem

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

时间: 2024-12-29 23:45:47

six solutions to a single symmetrical problem的相关文章

Jeff Somers&#39;s N Queens Solutions 最快的n皇后算法

1 /* Jeff Somers 2 * 3 * Copyright (c) 2002 4 * 5 * [email protected] 6 * or 7 * [email protected] 8 * 9 * April, 2002 10 * 11 * Program: nq 12 * 13 * Program to find number of solutions to the N queens problem. 14 * This program assumes a twos compl

app概念设计

翻译自:https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/AppDevelopmentProcess.html#//apple_ref/doc/uid/TP40011343-CH4-SW1 Ask yourself these questions as your define your concept: Who is your audience? Your app content

uva 11195 Another queen (用状态压缩解决N后问题)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2136 Problem A Another n-Queen Problem I guess the n-queen problem is known by every person who has studied backtracking. In this problem you s

[SinGuLaRiTy] COCI 2011~2012 #2

[SinGuLaRiTy-1007] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 测试题目 对于所有的题目:Time Limit:1s  |  Memory:256 MB 第一题:考试得分(score)[najboljih] [题目描述] 某单位举办了一次考试,考试有8道题,每道题的得分不一样.选手可以随便做,但最后只统计5道题的得分.现在给出选手每道题的得分,求出他最后的得分最大是多少? [输入] 8行,每行一个正整数X(0<=X<

SQL Fundamentals: Basic SELECT statement基本的select语句

Basic SELECT statement基本的select语句 The basic syntax for a SELECT statement is presented below. SELECT语句的基本语法如下. |:多选一 []:可选择的内容 {}:多选一 没有被{}括起来的是必选 SELECT [DISTINCT | ALL] {* | select_list} FROM {table_name [alias] | view_name}     [{table_name [alias

如何学习iOS开发?iOS Developer Library足矣!

记得上高中的时候,寄信请教二哥学习经验,二哥来信介绍学习经验说:资料书要快速阅读,把书上的题做完,然后再买几套资料书(习题集)继续练习. 这是二哥的经验,因为他自学能力强,可以消化多套资料书. 我仿照二哥的学习经验,高中一学期买3.4套资料书,东一榔头西一棒,哪套都没学精学透. 实践证明,资料不在多,而在精,人的精力有限,用最宝贵的时间学习最经典.优秀的资料,和风险管理纳税评估的理念也是想通的,2/8原则,用百分之八十的精力,专注处理最重要.最优先.最容易出成效的百分之二十的事情. iOS De

CaptureManagerSDK

Simple SDK for capturing, recording and streaming video and audio from web-cams on Windows OS by Windows Media Foundation. Download CaptureManagerSDK-1.0.0.zip - 1.1 MB Download CaptureManagerSDK-1.1.0.zip - 1.1 MB Download CaptureManagerSDK-1.2.0_be

(转)The Road to TensorFlow

Stephen Smith's Blog All things Sage 300… The Road to TensorFlow – Part 7: Finally Some Code leave a comment » Introduction Well after a long journey through Linux, Python, Python Libraries, the Stock Market, an Introduction to Neural Networks and tr

HNU11374:GREAT+SWERC=PORTO(DFS)

Problem description We want to have a great SWERC at Porto this year and we approached this challenge in several ways. We even framed it as a word addition problem, similar to the classic SEND+MORE=MONEY, where each letter stands for a single digit (