如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string,如aba,或者abba。本题是这样的,给定输入一个字符串,要求输出一个子串,使得子串是最长的padromic string。
下边演示3种思路
1.两侧比较法
以abba这样一个字符串为例来看,abba中,一共有偶数个字,第1位=倒数第1位,第2位=倒数第2位......第N位=倒数第...
分类:
编程语言 时间:
2014-07-08 17:49:03
阅读次数:
238
最长回文
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7317 Accepted Submission(s): 2500
Problem Description
给出一个只由小写英文字符a,b,c...y,z组成...
分类:
其他好文 时间:
2014-07-08 15:24:02
阅读次数:
195
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.
给定一个字符串S,找出其中的最长回文...
分类:
其他好文 时间:
2014-07-06 00:15:15
阅读次数:
240
现给定一个已知的字符串str[],现在想要在O(n)的时间复杂度之内求出一个最长的回文子字符串(正着和倒着顺序读一致)。Manacher最早发现了可以用O(n)的时间复杂度来解决该问题,所以这种方法称之为Manacher算法。#include using namespace std;int min(...
分类:
其他好文 时间:
2014-07-01 13:11:29
阅读次数:
245
PalindromeTime Limit:15000MSMemory Limit:65536KTotal Submissions:3695Accepted:1338DescriptionAndy the smart computer science student was attending an ...
分类:
其他好文 时间:
2014-06-27 13:46:53
阅读次数:
296
Longest Palindromic Substring:Given a stringS, find the longest palindromic substring inS.You may assume that the maximum length ofSis 1000, and there...
分类:
其他好文 时间:
2014-06-21 07:36:18
阅读次数:
227
void LP(const string &str, int **c){ int m =
str.size(); for(int i=0; i<m; ++i) c[i][i] = 1; for(int d=1; d<m; ++d) {
// 自底向上 ...
分类:
其他好文 时间:
2014-06-13 17:32:10
阅读次数:
234
最长回文字串,相信做过Palindrome Partitioning II 这个题的同学应该可以很快做出来。没错,这个题还可以使用动态规划方法得到一个时间复杂度为O(n^2)的解法,当然如果你想要更好的时间复杂度的算法也是有的。好的,我们先来看看时间复杂度为O(n^2)的算法。
代码实现--动态规划O(n^2)
相信如果你在网上看过了别人的算法,你会发现我的算法是最简洁的。哈哈,这个题需要注意的是如果你用惯了vector的话,你这里肯定会得到超时的提示...
...代码二--复杂度为O(n)的算法...
分类:
其他好文 时间:
2014-06-07 12:08:43
阅读次数:
224
一般求回文子串用的是Manacher算法,但是该算法只是简单判断回文,该题目中需要去除掉空格和标点,所以,自己用了动态规划(加剪枝,取出空号等)。
代码如下:
//最长回文子串 动态规划
#include
#include
#include
#include //for tolower
#define MAXSIZE 5000
char str[MAXSIZE];//="Confucius...
分类:
其他好文 时间:
2014-05-21 16:00:30
阅读次数:
211
【题目】
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.
【题意】
题意是找出字符串S中最长回文子串,S最长为1000,保证有唯一解
【思路】
原字符串用特殊字符#间隔,如下所示:
#a...
分类:
其他好文 时间:
2014-05-15 03:31:25
阅读次数:
299